/var/www/hkosl.com/aga/wp-content/plugins/smart-slider-3/library/smartslider/libraries/import.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<?php
N2Loader
::import('libraries.zip.reader');
N2Loader::import('libraries.backup''smartslider');

class 
N2SmartSliderImport {

    
/**
     * @var N2SmartSliderBackup
     */
    
private $backup;
    private 
$imageTranslation = array();
    private 
$sectionTranslation = array();

    private 
$sliderId 0;

    private 
$restore false;

    public function 
enableRestore() {
        
$this->restore true;
    }

    public function 
import($filePathOrData$groupID 0$imageImportMode 'clone'$linkedVisuals 1$isFilePath true) {
        if (!
$isFilePath) {
            
$tmp N2Filesystem::tempnam();
            
file_put_contents($tmp$filePathOrData);
            
$filePathOrData $tmp;
        }
        
$importData N2ZipReader::read($filePathOrData);
        if (!
$isFilePath) {
            @
unlink($tmp);
        }
        if (!
is_array($importData)) {
            
N2Message::error(n2_('The importing failed at the unzipping part.'));

            return 
false;
        } else if (!isset(
$importData['data'])) {
            if (
array_key_exists("slider.ss2"$importData)) {
                
N2Message::error(n2_('You can\'t import sliders from Smart Slider 2.'));
            } else if(empty(
$importData)){
                
N2Message::error(n2_('Export file corrupted! Slider data is missing.'));
            }

            return 
false;
        }

        
$this->backup unserialize($importData['data']);


        if (!empty(
$this->backup->slider['type']) && $this->backup->slider['type'] == 'group') {
            
// Groups can not be imported into groups
            
$groupID 0;
        }

        
$this->sectionTranslation = array();
        
$this->importVisuals($this->backup->visuals$linkedVisuals);


        
$sliderModel = new N2SmartsliderSlidersModel();


        if (
$this->restore) {
            
$this->sliderId $sliderModel->restore($this->backup->slider$groupID);
        } else {
            
$this->sliderId $sliderModel->import($this->backup->slider$groupID);
        }

        if (!
$this->sliderId) {
            return 
false;
        }

        switch (
$imageImportMode) {
            case 
'clone':
                
$images     = isset($importData['images']) ? $importData['images'] : array();
                
$imageStore = new N2StoreImage('slider' $this->sliderIdtrue);
                foreach (
$images AS $file => $content) {
                    
$localImage $imageStore->makeCache($file$content);
                    if (
$localImage) {
                        
$this->imageTranslation[$file] = N2ImageHelper::dynamic(N2Uri::pathToUri($localImage));
                    } else {
                        
$this->imageTranslation[$file] = $file;
                    }
                    if (!
$this->imageTranslation[$file]) {
                        
$this->imageTranslation[$file] = array_search($file$this->backup->imageTranslation);
                    }
                }
                break;
            case 
'update':
                
$keys   array_keys($this->backup->NextendImageHelper_Export);
                
$values array_values($this->backup->NextendImageHelper_Export);
                foreach (
$this->backup->imageTranslation AS $image => $value) {
                    
$this->imageTranslation[$value] = str_replace($keys$values$image);
                }
                break;
            default:
                break;
        }
        if (!empty(
$this->backup->slider['thumbnail'])) {
            
$sliderModel->setThumbnail($this->sliderId$this->fixImage($this->backup->slider['thumbnail']));
        }

        foreach (
$this->backup->NextendImageManager_ImageData AS $image => $data) {
            
$data['tablet']['image'] = $this->fixImage($data['tablet']['image']);
            
$data['mobile']['image'] = $this->fixImage($data['mobile']['image']);
            
$fixedImage              $this->fixImage($image);
            if (!
N2ImageManager::hasImageData($fixedImage)) {
                
N2ImageManager::addImageData($this->fixImage($image), $data);
            }
        }

        if (empty(
$this->backup->slider['type'])) {
            
$this->backup->slider['type'] = 'simple';
        }


        if (
$this->backup->slider['type'] == 'group') {
            
/**
             * Import the sliders for the group!
             */
            
foreach ($importData['sliders'] AS $k => $slider) {
                
$import = new N2SmartSliderImport();
                if (
$this->restore) {
                    
$import->enableRestore();
                }
                
$import->import($slider$this->sliderId$imageImportMode$linkedVisualsfalse);
            }
        } else {

            unset(
$importData);

            
$sliderType N2SSPluginSliderType::getSliderType($this->backup->slider['type']);
            
$sliderType->import($this$this->backup->slider);


            
$enabledWidgets = array();
            
$widgetGroups   N2SmartSliderWidgets::getGroups();

            
$params $this->backup->slider['params'];
            foreach (
$widgetGroups AS $groupName => $group) {
                
$widgetName $params->get('widget' $groupName);
                if (
$widgetName && $widgetName != 'disabled') {
                    
$widget $group->getWidget($widgetName);
                    if (
$widget) {
                        
$enabledWidgets[$groupName] = $widget;
                    }
                }
            }

            foreach (
$enabledWidgets AS $k => $widget) {
                
$params->fillDefault($widget->getDefaults());

                
$widget->prepareImport($this$params);
            }


            
$sliderModel->importUpdate($this->sliderId$params);

            
$generatorTranslation = array();
            
N2Loader::import("models.generator""smartslider");
            
$generatorModel = new N2SmartsliderGeneratorModel();
            foreach (
$this->backup->generators as $generator) {
                
$generatorTranslation[$generator['id']] = $generatorModel->import($generator);
            }


            
$slidesModel = new N2SmartsliderSlidesModel();
            for (
$i 0$i count($this->backup->slides); $i++) {
                
$slide              $this->backup->slides[$i];
                
$slide['params']    = new N2Data($slide['params'], true);
                
$slide['thumbnail'] = $this->fixImage($slide['thumbnail']);
                
$slide['params']->set('backgroundImage'$this->fixImage($slide['params']->get('backgroundImage')));
                
$slide['params']->set('ligthboxImage'$this->fixImage($slide['params']->get('ligthboxImage')));

                if (
$slide['params']->has('link')) {
                    
// Compatibility fix for the old SS3 import files
                    
$slide['params']->set('link'$this->fixLightbox($slide['params']->get('link')));
                }
                if (
$slide['params']->has('href')) {
                    
$slide['params']->set('href'$this->fixLightbox($slide['params']->get('href')));
                }

                
$layers json_decode($slide['slide'], true);

                
self::prepareImportLayer($this$layers);

                
$slide['slide'] = json_encode($layers);

                if (isset(
$generatorTranslation[$slide['generator_id']])) {
                    
$slide['generator_id'] = $generatorTranslation[$slide['generator_id']];
                }
                
$slidesModel->import($slide$this->sliderId);
            }
        }

        return 
$this->sliderId;
    }

    
/**
     * @param N2SmartSliderImport $import
     * @param array               $layers
     */
    
public static function prepareImportLayer($import, &$layers) {
        for (
$i 0$i count($layers); $i++) {

            if (isset(
$layers[$i]['type'])) {
                switch (
$layers[$i]['type']) {
                    case 
'content':
                        
N2SSSlideComponentContent::prepareImport($import$layers[$i]);
                        break;
                    case 
'row':
                        
N2SSSlideComponentRow::prepareImport($import$layers[$i]);
                        break;
                    case 
'col':
                        
N2SSSlideComponentCol::prepareImport($import$layers[$i]);
                        break;
                    case 
'group':
                        
N2SSSlideComponentGroup::prepareImport($import$layers[$i]);
                        break;
                    default:
                        
N2SSSlideComponentLayer::prepareImport($import$layers[$i]);
                }
            } else {
                
N2SSSlideComponentLayer::prepareImport($import$layers[$i]);
            }
        }
    }

    public function 
fixImage($image) {
        if (isset(
$this->backup->imageTranslation[$image]) && isset($this->imageTranslation[$this->backup->imageTranslation[$image]])) {
            return 
$this->imageTranslation[$this->backup->imageTranslation[$image]];
        }

        return 
$image;
    }

    public function 
fixSection($idOrRaw) {
        if (isset(
$this->sectionTranslation[$idOrRaw])) {
            return 
$this->sectionTranslation[$idOrRaw];
        }

        return 
$idOrRaw;
    }

    public function 
fixLightbox($url) {
        
preg_match('/^([a-zA-Z]+)\[(.*)](.*)/'$url$matches);
        if (!empty(
$matches) && $matches[1] == 'lightbox') {
            
$images    explode(','$matches[2]);
            
$newImages = array();
            foreach (
$images AS $image) {
                
$newImages[] = $this->fixImage($image);
            }
            
$url 'lightbox[' implode(','$newImages) . ']' $matches[3];
        }

        return 
$url;
    }

    private function 
importVisuals($records$linkedVisuals) {
        if (
count($records)) {
            if (!
$linkedVisuals) {
                foreach (
$records AS $record) {
                    
$this->sectionTranslation[$record['id']] = $record['value'];
                }
            } else {
                
$sets = array();
                foreach (
$records AS $record) {
                    
$storage N2Base::getApplication($record['application'])->storage;
                    if (!isset(
$sets[$record['application'] . '_' $record['section']])) {
                        
$sets[$record['application'] . '_' $record['section']] = $storage->add($record['section'] . 'set'''$this->backup->slider['title']);
                    }
                    
$this->sectionTranslation[$record['id']] = $storage->add($record['section'], $sets[$record['application'] . '_' $record['section']], $record['value']);
                }
            }
        }
    }
}