| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @package Device | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @author Iurii Makukh | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @copyright Copyright (c) 2017, Iurii Makukh | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | namespace gplcart\modules\device; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use gplcart\core\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use gplcart\core\helpers\Session; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use gplcart\core\Library; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use gplcart\core\Module; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use LogicException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * Main class for Device module | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | class Main | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * Module class instance | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @var \gplcart\core\Module $module | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     protected $module; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * Library class instance | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @var \gplcart\core\Library $library | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     protected $library; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * Session helper class instance | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @var \gplcart\core\helpers\Session $session | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     protected $session; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @param Module $module | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @param Library $library | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @param Session $session | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     public function __construct(Module $module, Library $library, Session $session) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $this->module = $module; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $this->library = $library; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $this->session = $session; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * Implements hook "library.list" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @param array $libraries | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     public function hookLibraryList(array &$libraries) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $libraries['mobile_detect'] = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             'name' => 'Mobile Detect', // @text | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             'description' => 'A lightweight PHP class for detecting mobile devices', // @text | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             'url' => 'https://github.com/serbanghita/Mobile-Detect', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             'download' => 'https://github.com/serbanghita/Mobile-Detect/archive/2.8.25.zip', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             'type' => 'php', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             'version_source' => array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                 'file' => 'vendor/mobiledetect/mobiledetectlib/Mobile_Detect.php', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                 'pattern' => '/.*VERSION.*(\\d+\\.+\\d+\\.+\\d+)/', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                 'lines' => 100 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             'module' => 'device', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             'files' => array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                 'vendor/mobiledetect/mobiledetectlib/Mobile_Detect.php' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * Implements hook "route.list" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @param array $routes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     public function hookRouteList(array &$routes) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $routes['admin/module/settings/device'] = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             'access' => 'module_edit', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             'handlers' => array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                 'controller' => array('gplcart\\modules\\device\\controllers\\Settings', 'editSettings') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * Implements hook "theme" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * @param Controller $controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     public function hookTheme(Controller $controller) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         $this->switchTheme($controller); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * Implements hook "module.enable.after" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     public function hookModuleEnableAfter() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         $this->library->clearCache(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * Implements hook "module.disable.after" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     public function hookModuleDisableAfter() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         $this->library->clearCache(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * Implements hook "module.install.after" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     public function hookModuleInstallAfter() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         $this->library->clearCache(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      * Implements hook "module.uninstall.after" | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 128 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |     public function hookModuleUninstallAfter() | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |         $this->library->clearCache(); | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      * Returns a device type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     public function getDeviceType() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         $device = $this->session->get('device'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         if (empty($device)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |             $detector = $this->getLibrary(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |             if ($detector->isMobile()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |                 $device = 'mobile'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |             } else if ($detector->isTablet()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |                 $device = 'tablet'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |                 $device = 'desktop'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |             $this->session->set('device', $device); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         return $device; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |      * Returns the mobile detector instance | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |      * @return \Mobile_Detect | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |      * @throws LogicException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |     public function getLibrary() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |         $this->library->load('mobile_detect'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |         if (class_exists('Mobile_Detect')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |             return new \Mobile_Detect; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |         throw new LogicException('Class Mobile_Detect not found'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |      * Switch the current theme | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |      * @param Controller $controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |     protected function switchTheme(Controller $controller) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |         if (!$controller->isInternalRoute()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |             try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |                 $device = $this->getDeviceType(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |                 $store_id = $controller->getStoreId(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |                 $settings = $this->module->getSettings('device'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |                 if (!$controller->isBackend() && $device !== 'desktop' && !empty($settings['theme'][$store_id][$device])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |                     $theme = $settings['theme'][$store_id][$device]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |                     if ($this->module->isEnabled($theme)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |                         $controller->setCurrentTheme($theme); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |             } catch (Exception $ex) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |                 trigger_error($ex->getMessage()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 203 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 204 |  |  |  |