PageRenderTime 126ms CodeModel.GetById 95ms app.highlight 25ms RepoModel.GetById 1ms app.codeStats 0ms

/sondage/admin/import_resources_zip.php

https://bitbucket.org/bontiv/insomnia
PHP | 618 lines | 506 code | 68 blank | 44 comment | 62 complexity | b086af5752f42a0ff1c8466178bc5f55 MD5 | raw file
  1<?php
  2/*
  3 * LimeSurvey
  4 * Copyright (C) 2007 The LimeSurvey Project Team / Carsten Schmitz
  5 * All rights reserved.
  6 * License: GNU/GPL License v2 or later, see LICENSE.php
  7 * LimeSurvey is free software. This version may have been modified pursuant
  8 * to the GNU General Public License, and as distributed it includes or
  9 * is derivative of works licensed under the GNU General Public License or
 10 * other free or open source software licenses.
 11 * See COPYRIGHT.php for copyright notices and details.
 12 *
 13 * $Id: import_resources_zip.php 10925 2011-09-02 14:12:02Z c_schmitz $
 14 */
 15
 16
 17include_once("login_check.php");
 18
 19if (!isset($surveyid))
 20{
 21    returnglobal('sid');
 22}
 23
 24if (!isset($lid))
 25{
 26    returnglobal('lid');
 27}
 28
 29
 30
 31if ($action == "importsurveyresources" && $surveyid) {
 32    $importsurveyresourcesoutput = "<div class='header ui-widget-header'>".$clang->gT("Import survey resources")."</div>\n";
 33    $importsurveyresourcesoutput .= "<div class='messagebox ui-corner-all'>";
 34
 35    if ($demoModeOnly === true)
 36    {
 37        $importsurveyresourcesoutput .= "<div class=\"warningheader\">".$clang->gT("Error")."</div><br />\n";
 38        $importsurveyresourcesoutput .= $clang->gT("Demo Mode Only: Uploading file is disabled in this system.")."<br /><br />\n";
 39        $importsurveyresourcesoutput .= "<input type='submit' value='".$clang->gT("Back")."' onclick=\"window.open('$scriptname?action=editsurvey&amp;sid=$surveyid', '_top')\" />\n";
 40        $importsurveyresourcesoutput .= "</div>\n";
 41        return;
 42    }
 43
 44    require("classes/phpzip/phpzip.inc.php");
 45    $zipfile=$_FILES['the_file']['tmp_name'];
 46    $z = new PHPZip();
 47
 48    // Create temporary directory
 49    // If dangerous content is unzipped
 50    // then no one will know the path
 51    $extractdir=tempdir($tempdir);
 52    $basedestdir = $uploaddir."/surveys";
 53    $destdir=$basedestdir."/$surveyid/";
 54
 55    if (!is_writeable($basedestdir))
 56    {
 57        $importsurveyresourcesoutput .= "<div class=\"warningheader\">".$clang->gT("Error")."</div><br />\n";
 58        $importsurveyresourcesoutput .= sprintf ($clang->gT("Incorrect permissions in your %s folder."),$basedestdir)."<br /><br />\n";
 59        $importsurveyresourcesoutput .= "<input type='submit' value='".$clang->gT("Back")."' onclick=\"window.open('$scriptname?action=editsurvey&sid=$surveyid', '_top')\" />\n";
 60        $importsurveyresourcesoutput .= "</div>\n";
 61        return;
 62    }
 63
 64    if (!is_dir($destdir))
 65    {
 66        mkdir($destdir);
 67    }
 68
 69    $aImportedFilesInfo=null;
 70    $aErrorFilesInfo=null;
 71
 72
 73    if (is_file($zipfile))
 74    {
 75        $importsurveyresourcesoutput .= "<div class=\"successheader\">".$clang->gT("Success")."</div><br />\n";
 76        $importsurveyresourcesoutput .= $clang->gT("File upload succeeded.")."<br /><br />\n";
 77        $importsurveyresourcesoutput .= $clang->gT("Reading file..")."<br /><br />\n";
 78
 79        if ($z->extract($extractdir,$zipfile) != 'OK')
 80        {
 81            $importsurveyresourcesoutput .= "<div class=\"warningheader\">".$clang->gT("Error")."</div><br />\n";
 82            $importsurveyresourcesoutput .= $clang->gT("This file is not a valid ZIP file archive. Import failed.")."<br /><br />\n";
 83            $importsurveyresourcesoutput .= "<input type='submit' value='".$clang->gT("Back")."' onclick=\"window.open('$scriptname?action=editsurvey&sid=$surveyid', '_top')\" />\n";
 84            $importsurveyresourcesoutput .= "</div>\n";
 85            return;
 86        }
 87
 88        // now read tempdir and copy authorized files only
 89        $dh = opendir($extractdir);
 90        while($direntry = readdir($dh))
 91        {
 92            if (($direntry!=".")&&($direntry!=".."))
 93            {
 94                if (is_file($extractdir."/".$direntry))
 95                { // is  a file
 96                    $extfile = substr(strrchr($direntry, '.'),1);
 97                    if  (!(stripos(','.$allowedresourcesuploads.',',','.$extfile.',') === false))
 98                    { //Extension allowed
 99                        if (!copy($extractdir."/".$direntry, $destdir.$direntry))
100                        {
101                            $aErrorFilesInfo[]=Array(
102								"filename" => $direntry,
103								"status" => $clang->gT("Copy failed")
104                            );
105                            unlink($extractdir."/".$direntry);
106
107                        }
108                        else
109                        {
110                            $aImportedFilesInfo[]=Array(
111								"filename" => $direntry,
112								"status" => $clang->gT("OK")
113                            );
114                            unlink($extractdir."/".$direntry);
115                        }
116                    }
117
118                    else
119                    { // Extension forbidden
120                        $aErrorFilesInfo[]=Array(
121							"filename" => $direntry,
122							"status" => $clang->gT("Error")." (".$clang->gT("Forbidden Extension").")"
123							);
124							unlink($extractdir."/".$direntry);
125                    }
126                } // end if is_file
127            } // end if ! . or ..
128        } // end while read dir
129
130
131        //Delete the temporary file
132        unlink($zipfile);
133        //Delete temporary folder
134        rmdir($extractdir);
135
136        // display summary
137        $okfiles = 0;
138        $errfiles= 0;
139        $ErrorListHeader = "";
140        $ImportListHeader = "";
141        if (is_null($aErrorFilesInfo) && !is_null($aImportedFilesInfo))
142        {
143            $status=$clang->gT("Success");
144            $statusClass='successheader';
145            $okfiles = count($aImportedFilesInfo);
146            $ImportListHeader .= "<br /><strong><u>".$clang->gT("Imported Files List").":</u></strong><br />\n";
147        }
148        elseif (is_null($aErrorFilesInfo) && is_null($aImportedFilesInfo))
149        {
150            $importsurveyresourcesoutput .= "<div class=\"warningheader\">".$clang->gT("Error")."</div><br />\n";
151            $importsurveyresourcesoutput .= $clang->gT("This ZIP archive contains no valid Resources files. Import failed.")."<br /><br />\n";
152            $importsurveyresourcesoutput .= $clang->gT("Remember that we do not support subdirectories in ZIP archives.")."<br /><br />\n";
153            $importsurveyresourcesoutput .= "<input type='submit' value='".$clang->gT("Back")."' onclick=\"window.open('$scriptname?action=editsurvey&sid=$surveyid', '_top')\" />\n";
154            $importsurveyresourcesoutput .= "</div>\n";
155            return;
156
157        }
158        elseif (!is_null($aErrorFilesInfo) && !is_null($aImportedFilesInfo))
159        {
160            $status=$clang->gT("Partial");
161            $statusClass='partialheader';
162            $okfiles = count($aImportedFilesInfo);
163            $errfiles = count($aErrorFilesInfo);
164            $ErrorListHeader .= "<br /><strong><u>".$clang->gT("Error Files List").":</u></strong><br />\n";
165            $ImportListHeader .= "<br /><strong><u>".$clang->gT("Imported Files List").":</u></strong><br />\n";
166        }
167        else
168        {
169            $status=$clang->gT("Error");
170            $statusClass='warningheader';
171            $errfiles = count($aErrorFilesInfo);
172            $ErrorListHeader .= "<br /><strong><u>".$clang->gT("Error Files List").":</u></strong><br />\n";
173        }
174
175        $importsurveyresourcesoutput .= "<strong>".$clang->gT("Imported Resources for")." SID:</strong> $surveyid<br /><br />\n";
176        $importsurveyresourcesoutput .= "<div class=\"".$statusClass."\">".$status."</div><br />\n";
177        $importsurveyresourcesoutput .= "<strong><u>".$clang->gT("Resources Import Summary")."</u></strong><br />\n";
178        $importsurveyresourcesoutput .= "".$clang->gT("Total Imported files").": $okfiles<br />\n";
179        $importsurveyresourcesoutput .= "".$clang->gT("Total Errors").": $errfiles<br />\n";
180        $importsurveyresourcesoutput .= $ImportListHeader;
181        foreach ($aImportedFilesInfo as $entry)
182        {
183            $importsurveyresourcesoutput .= "\t<li>".$clang->gT("File").": ".$entry["filename"]."</li>\n";
184        }
185        if (!is_null($aImportedFilesInfo))
186        {
187            $importsurveyresourcesoutput .= "\t</ul><br />\n";
188        }
189        $importsurveyresourcesoutput .= $ErrorListHeader;
190        foreach ($aErrorFilesInfo as $entry)
191        {
192            $importsurveyresourcesoutput .= "\t<li>".$clang->gT("File").": ".$entry['filename']." (".$entry['status'].")</li>\n";
193        }
194        if (!is_null($aErrorFilesInfo))
195        {
196            $importsurveyresourcesoutput .= "\t</ul><br />\n";
197        }
198    }
199    else
200    {
201        $importsurveyresourcesoutput .= "<div class=\"warningheader\">".$clang->gT("Error")."</div><br />\n";
202        $importsurveyresourcesoutput .= sprintf ($clang->gT("An error occurred uploading your file. This may be caused by incorrect permissions in your %s folder."),$basedestdir)."<br /><br />\n";
203        $importsurveyresourcesoutput .= "<input type='submit' value='".$clang->gT("Back")."' onclick=\"window.open('$scriptname?action=editsurvey&sid=$surveyid', '_top')\" />\n";
204        $importsurveyresourcesoutput .= "</div>\n";
205        return;
206    }
207    $importsurveyresourcesoutput .= "<input type='submit' value='".$clang->gT("Back")."' onclick=\"window.open('$scriptname?action=editsurvey&sid=$surveyid', '_top')\" />\n";
208    $importsurveyresourcesoutput .= "</div>\n";
209}
210
211
212
213if ($action == "importlabelresources" && $lid)
214{
215    $importlabelresourcesoutput = "<div class='header ui-widget-header'>".$clang->gT("Import Label Set")."</div>\n";
216    $importlabelresourcesoutput .= "<div class='messagebox ui-corner-all'>";
217
218    if ($demoModeOnly === true)
219    {
220        $importlabelresourcesoutput .= "<div class=\"warningheader\">".$clang->gT("Error")."</div><br />\n";
221        $importlabelresourcesoutput .= sprintf ($clang->gT("Demo mode only: Uploading files is disabled in this system."),$basedestdir)."<br /><br />\n";
222        $importlabelresourcesoutput .= "<input type='submit' value='".$clang->gT("Main Admin Screen")."' onclick=\"window.open('$scriptname?action=labels&lid=$lid', '_top')\" />\n";
223        $importlabelresourcesoutput .= "</div>\n";
224        return;
225    }
226
227    require("classes/phpzip/phpzip.inc.php");
228    //$the_full_file_path = $tempdir . "/" . $_FILES['the_file']['name'];
229    $zipfile=$_FILES['the_file']['tmp_name'];
230    $z = new PHPZip();
231    // Create temporary directory
232    // If dangerous content is unzipped
233    // then no one will know the path
234    $extractdir=tempdir($tempdir);
235    $basedestdir = $publicdir."/upload/labels";
236    $destdir=$basedestdir."/$lid/";
237
238    if (!is_writeable($basedestdir))
239    {
240        $importlabelresourcesoutput .= "<div class=\"warningheader\">".$clang->gT("Error")."</div><br />\n";
241        $importlabelresourcesoutput .= sprintf ($clang->gT("Incorrect permissions in your %s folder."),$basedestdir)."<br /><br />\n";
242        $importlabelresourcesoutput .= "<input type='submit' value='".$clang->gT("Main Admin Screen")."' onclick=\"window.open('$scriptname?action=labels&lid=$lid', '_top')\" />\n";
243        $importlabelresourcesoutput .= "</div>\n";
244        return;
245    }
246
247    if (!is_dir($destdir))
248    {
249        mkdir($destdir);
250    }
251
252    $aImportedFilesInfo=null;
253    $aErrorFilesInfo=null;
254
255
256    if (is_file($zipfile))
257    {
258        $importlabelresourcesoutput .= "<div class=\"successheader\">".$clang->gT("Success")."</div><br />\n";
259        $importlabelresourcesoutput .= $clang->gT("File upload succeeded.")."<br /><br />\n";
260        $importlabelresourcesoutput .= $clang->gT("Reading file..")."<br /><br />\n";
261
262        if ($z->extract($extractdir,$zipfile) != 'OK')
263        {
264            $importlabelresourcesoutput .= "<div class=\"warningheader\">".$clang->gT("Error")."</div><br />\n";
265            $importlabelresourcesoutput .= $clang->gT("This file is not a valid ZIP file archive. Import failed.")."<br /><br />\n";
266            $importlabelresourcesoutput .= "<input type='submit' value='".$clang->gT("Main Admin Screen")."' onclick=\"window.open('$scriptname?action=labels&lid=$lid', '_top')\" />\n";
267            $importlabelresourcesoutput .= "</div>\n";
268            return;
269        }
270
271        // now read tempdir and copy authorized files only
272        $dh = opendir($extractdir);
273        while($direntry = readdir($dh))
274        {
275            if (($direntry!=".")&&($direntry!=".."))
276            {
277                if (is_file($extractdir."/".$direntry))
278                { // is  a file
279                    $extfile = substr(strrchr($direntry, '.'),1);
280                    if  (!(stripos(','.$allowedresourcesuploads.',',','.$extfile.',') === false))
281                    { //Extension allowed
282                        if (!copy($extractdir."/".$direntry, $destdir.$direntry))
283                        {
284                            $aErrorFilesInfo[]=Array(
285								"filename" => $direntry,
286								"status" => $clang->gT("Copy failed")
287                            );
288                            unlink($extractdir."/".$direntry);
289
290                        }
291                        else
292                        {
293                            $aImportedFilesInfo[]=Array(
294								"filename" => $direntry,
295								"status" => $clang->gT("OK")
296                            );
297                            unlink($extractdir."/".$direntry);
298                        }
299                    }
300
301                    else
302                    { // Extension forbidden
303                        $aErrorFilesInfo[]=Array(
304							"filename" => $direntry,
305							"status" => $clang->gT("Error")." (".$clang->gT("Forbidden Extension").")"
306							);
307							unlink($extractdir."/".$direntry);
308                    }
309                } // end if is_file
310            } // end if ! . or ..
311        } // end while read dir
312
313
314        //Delete the temporary file
315        unlink($zipfile);
316        //Delete temporary folder
317        rmdir($extractdir);
318
319        // display summary
320        $okfiles = 0;
321        $errfiles= 0;
322        $ErrorListHeader .= "";
323        $ImportListHeader .= "";
324        if (is_null($aErrorFilesInfo) && !is_null($aImportedFilesInfo))
325        {
326            $status=$clang->gT("Success");
327            $statusClass='successheader';
328            $okfiles = count($aImportedFilesInfo);
329            $ImportListHeader .= "<br /><strong><u>".$clang->gT("Imported Files List").":</u></strong><br />\n";
330        }
331        elseif (is_null($aErrorFilesInfo) && is_null($aImportedFilesInfo))
332        {
333            $importlabelresourcesoutput .= "<div class=\"warningheader\">".$clang->gT("Error")."</div><br />\n";
334            $importlabelresourcesoutput .= $clang->gT("This ZIP archive contains no valid Resources files. Import failed.")."<br /><br />\n";
335            $importlabelresourcesoutput .= $clang->gT("Remember that we do not support subdirectories in ZIP archives.")."<br /><br />\n";
336            $importlabelresourcesoutput .= "<input type='submit' value='".$clang->gT("Main Admin Screen")."' onclick=\"window.open('$scriptname?action=labels&lid=$lid', '_top')\" />\n";
337            $importlabelresourcesoutput .= "</div>\n";
338            return;
339        }
340        elseif (!is_null($aErrorFilesInfo) && !is_null($aImportedFilesInfo))
341        {
342            $status=$clang->gT("Partial");
343            $statusClass='partialheader';
344            $okfiles = count($aImportedFilesInfo);
345            $errfiles = count($aErrorFilesInfo);
346            $ErrorListHeader .= "<br /><strong><u>".$clang->gT("Error Files List").":</u></strong><br />\n";
347            $ImportListHeader .= "<br /><strong><u>".$clang->gT("Imported Files List").":</u></strong><br />\n";
348        }
349        else
350        {
351            $status=$clang->gT("Error");
352            $statusClass='warningheader';
353            $errfiles = count($aErrorFilesInfo);
354            $ErrorListHeader .= "<br /><strong><u>".$clang->gT("Error Files List").":</u></strong><br />\n";
355        }
356
357        $importlabelresourcesoutput .= "<strong>".$clang->gT("Imported Resources for")." LID:</strong> $lid<br /><br />\n";
358        $importlabelresourcesoutput .= "<div class=\"".$statusClass."\">".$status."</div><br />\n";
359        $importlabelresourcesoutput .= "<strong><u>".$clang->gT("Resources Import Summary")."</u></strong><br />\n";
360        $importlabelresourcesoutput .= "".$clang->gT("Total Imported files").": $okfiles<br />\n";
361        $importlabelresourcesoutput .= "".$clang->gT("Total Errors").": $errfiles<br />\n";
362        $importlabelresourcesoutput .= $ImportListHeader;
363        foreach ($aImportedFilesInfo as $entry)
364        {
365            $importlabelresourcesoutput .= "\t<li>".$clang->gT("File").": ".$entry["filename"]."</li>\n";
366        }
367        if (!is_null($aImportedFilesInfo))
368        {
369            $importlabelresourcesoutput .= "\t</ul><br />\n";
370        }
371        $importlabelresourcesoutput .= $ErrorListHeader;
372        foreach ($aErrorFilesInfo as $entry)
373        {
374            $importlabelresourcesoutput .= "\t<li>".$clang->gT("File").": ".$entry['filename']." (".$entry['status'].")</li>\n";
375        }
376        if (!is_null($aErrorFilesInfo))
377        {
378            $importlabelresourcesoutput .= "\t</ul><br />\n";
379        }
380    }
381    else
382    {
383        $importlabelresourcesoutput .= "<div class=\"warningheader\">".$clang->gT("Error")."</div><br />\n";
384        $importlabelresourcesoutput .= sprintf ($clang->gT("An error occurred uploading your file. This may be caused by incorrect permissions in your %s folder."),$basedestdir)."<br /><br />\n";
385        $importlabelresourcesoutput .= "<input type='submit' value='".$clang->gT("Main Admin Screen")."' onclick=\"window.open('$scriptname?action=labels&lid=$lid', '_top')\" />\n";
386        $importlabelresourcesoutput .= "</div>\n";
387        return;
388    }
389    $importlabelresourcesoutput .= "<input type='submit' value='".$clang->gT("Back")."' onclick=\"window.open('$scriptname?action=labels&lid=$lid', '_top')\">\n";
390    $importlabelresourcesoutput .= "</div>\n";
391}
392
393
394
395if ($action == "templateupload")
396{
397    $importtemplateoutput = "<div class='header ui-widget-header'>".$clang->gT("Import template")."</div>\n";
398    $importtemplateoutput .= "<div class='messagebox ui-corner-all'>";
399
400    if ($demoModeOnly === true)
401    {
402        $importtemplateoutput .= "<div class=\"warningheader\">".$clang->gT("Error")."</div><br />\n";
403        $importtemplateoutput .= sprintf ($clang->gT("Demo mode: Uploading templates is disabled."),$basedestdir)."<br/><br/>\n";
404        $importtemplateoutput .= "<br/><input type=\"submit\" onclick=\"window.open('$scriptname?action=templates', '_top')\" value=\"".$clang->gT("Template Editor")."\"/>\n";
405        $importtemplateoutput .= "</div>\n";
406        return;
407    }
408
409    require("classes/phpzip/phpzip.inc.php");
410    //$the_full_file_path = $tempdir . "/" . $_FILES['the_file']['name'];
411    $zipfile=$_FILES['the_file']['tmp_name'];
412    $z = new PHPZip();
413    // Create temporary directory
414    // If dangerous content is unzipped
415    // then no one will know the path
416    $extractdir=tempdir($tempdir);
417    $basedestdir = $usertemplaterootdir;
418    $newdir=str_replace('.','',strip_ext(sanitize_paranoid_string($_FILES['the_file']['name'])));
419    $destdir=$basedestdir.'/'.$newdir.'/';
420
421    if (!is_writeable($basedestdir))
422    {
423        $importtemplateoutput .= "<div class=\"warningheader\">".$clang->gT("Error")."</div><br />\n";
424        $importtemplateoutput .= sprintf ($clang->gT("Incorrect permissions in your %s folder."),$basedestdir)."<br/><br/>\n";
425        $importtemplateoutput .= "<br/><input type=\"submit\" onclick=\"window.open('$scriptname?action=templates', '_top')\" value=\"".$clang->gT("Template Editor")."\"/>\n";
426        $importtemplateoutput .= "</div>\n";
427        return;
428    }
429
430    if (!is_dir($destdir))
431    {
432        mkdir($destdir);
433    }
434    else
435    {
436        $importtemplateoutput .= "<div class=\"warningheader\">".$clang->gT("Error")."</div><br />\n";
437        $importtemplateoutput .= sprintf ($clang->gT("Template '%s' does already exist."),$newdir)."<br/><br/>\n";
438        $importtemplateoutput .= "<br/><input type=\"submit\" onclick=\"window.open('$scriptname?action=templates', '_top')\" value=\"".$clang->gT("Template Editor")."\"/>\n";
439        $importtemplateoutput .= "</div>\n";
440        return;
441    }
442
443    $aImportedFilesInfo=array();
444    $aErrorFilesInfo=array();
445
446
447    if (is_file($zipfile))
448    {
449        $importtemplateoutput .= "<div class=\"successheader\">".$clang->gT("Success")."</div><br />\n";
450        $importtemplateoutput .= $clang->gT("File upload succeeded.")."<br /><br />\n";
451        $importtemplateoutput .= $clang->gT("Reading file..")."<br /><br />\n";
452
453        if ($z->extract($extractdir,$zipfile) != 'OK')
454        {
455            $importtemplateoutput .= "<div class=\"warningheader\">".$clang->gT("Error")."</div><br />\n";
456            $importtemplateoutput .= $clang->gT("This file is not a valid ZIP file archive. Import failed.")."<br/><br/>\n";
457            $importtemplateoutput .= "<br/><input type=\"submit\" onclick=\"window.open('$scriptname?action=templates', '_top')\" value=\"".$clang->gT("Template Editor")."\"/>\n";
458            $importtemplateoutput .= "</div>\n";
459            return;
460        }
461
462        $ErrorListHeader = "";
463        $ImportListHeader = "";
464
465        // now read tempdir and copy authorized files only
466        $dh = opendir($extractdir);
467        while($direntry = readdir($dh))
468        {
469            if (($direntry!=".")&&($direntry!=".."))
470            {
471                if (is_file($extractdir."/".$direntry))
472                { // is  a file
473                    $extfile = substr(strrchr($direntry, '.'),1);
474                    if  (!(stripos(','.$allowedresourcesuploads.',',','.$extfile.',') === false))
475                    { //Extension allowed
476                        if (!copy($extractdir."/".$direntry, $destdir.$direntry))
477                        {
478                            $aErrorFilesInfo[]=Array(
479                                "filename" => $direntry,
480                                "status" => $clang->gT("Copy failed")
481                            );
482                            unlink($extractdir."/".$direntry);
483
484                        }
485                        else
486                        {
487                            $aImportedFilesInfo[]=Array(
488                                "filename" => $direntry,
489                                "status" => $clang->gT("OK")
490                            );
491                            unlink($extractdir."/".$direntry);
492                        }
493                    }
494
495                    else
496                    { // Extension forbidden
497                        $aErrorFilesInfo[]=Array(
498                            "filename" => $direntry,
499                            "status" => $clang->gT("Error")." (".$clang->gT("Forbidden Extension").")"
500                            );
501                            unlink($extractdir."/".$direntry);
502                    }
503                } // end if is_file
504            } // end if ! . or ..
505        } // end while read dir
506
507
508        //Delete the temporary file
509        unlink($zipfile);
510        closedir($dh);
511        //Delete temporary folder
512        rmdir($extractdir);
513
514        // display summary
515        $okfiles = 0;
516        $errfiles= 0;
517        if (count($aErrorFilesInfo)==0 && count($aImportedFilesInfo)>0)
518        {
519            $status=$clang->gT("Success");
520            $statusClass='successheader';
521            $okfiles = count($aImportedFilesInfo);
522            $ImportListHeader .= "<br /><strong><u>".$clang->gT("Imported Files List").":</u></strong><br />\n";
523        }
524        elseif (count($aErrorFilesInfo)==0 && count($aImportedFilesInfo)==0)
525        {
526            $importtemplateoutput .= "<div class=\"warningheader\">".$clang->gT("Error")."</div><br />\n";
527            $importtemplateoutput .= $clang->gT("This ZIP archive contains no valid template files. Import failed.")."<br /><br />\n";
528            $importtemplateoutput .= $clang->gT("Remember that we do not support subdirectories in ZIP archives.")."<br/><br/>\n";
529            $importtemplateoutput .= "<br/><input type=\"submit\" onclick=\"window.open('$scriptname?action=templates', '_top')\" value=\"".$clang->gT("Template Editor")."\"/>\n";
530            $importtemplateoutput .= "</div>\n";
531            return;
532
533        }
534        elseif (count($aErrorFilesInfo)>0 && count($aImportedFilesInfo)>0)
535        {
536            $status=$clang->gT("Partial");
537            $statusClass='partialheader';
538            $okfiles = count($aImportedFilesInfo);
539            $errfiles = count($aErrorFilesInfo);
540            $ErrorListHeader .= "<br /><strong><u>".$clang->gT("Error Files List").":</u></strong><br />\n";
541            $ImportListHeader .= "<br /><strong><u>".$clang->gT("Imported Files List").":</u></strong><br />\n";
542        }
543        else
544        {
545            $status=$clang->gT("Error");
546            $statusClass='warningheader';
547            $errfiles = count($aErrorFilesInfo);
548            $ErrorListHeader .= "<br /><strong><u>".$clang->gT("Error Files List").":</u></strong><br />\n";
549        }
550
551        $importtemplateoutput .= "<strong>".$clang->gT("Imported template files for")."</strong> $lid<br /><br />\n";
552        $importtemplateoutput .= "<div class=\"".$statusClass."\">".$status."</div><br />\n";
553        $importtemplateoutput .= "<strong><u>".$clang->gT("Resources Import Summary")."</u></strong><br />\n";
554        $importtemplateoutput .= "".$clang->gT("Total Imported files").": $okfiles<br />\n";
555        $importtemplateoutput .= "".$clang->gT("Total Errors").": $errfiles<br />\n";
556        $importtemplateoutput .= $ImportListHeader;
557        foreach ($aImportedFilesInfo as $entry)
558        {
559            $importtemplateoutput .= "\t<li>".$clang->gT("File").": ".$entry["filename"]."</li>\n";
560        }
561        if (!is_null($aImportedFilesInfo))
562        {
563            $importtemplateoutput .= "\t</ul><br />\n";
564        }
565        $importtemplateoutput .= $ErrorListHeader;
566        foreach ($aErrorFilesInfo as $entry)
567        {
568            $importtemplateoutput .= "\t<li>".$clang->gT("File").": ".$entry['filename']." (".$entry['status'].")</li>\n";
569        }
570        if (!is_null($aErrorFilesInfo))
571        {
572            $importtemplateoutput .= "\t</ul><br />\n";
573        }
574    }
575    else
576    {
577        $importtemplateoutput .= "<div class=\"warningheader\">".$clang->gT("Error")."</div><br />\n";
578        $importtemplateoutput .= sprintf ($clang->gT("An error occurred uploading your file. This may be caused by incorrect permissions in your %s folder."),$basedestdir)."<br/><br/>\n";
579        $importtemplateoutput .= "<br/><input type=\"submit\" onclick=\"window.open('$scriptname?action=templates', '_top')\" value=\"".$clang->gT("Template Editor")."\"/>\n";
580        $importtemplateoutput .= "</div>\n";
581        return;
582    }
583    $importtemplateoutput .= "<input type='submit' value='".$clang->gT("Open imported template")."' onclick=\"window.open('$scriptname?action=templates&templatename=$newdir', '_top')\"/>\n";
584    $importtemplateoutput .= "</div>\n";
585}
586
587
588//---------------------
589// Comes from http://fr2.php.net/tempnam
590function tempdir($dir, $prefix='', $mode=0700)
591{
592    if (substr($dir, -1) != '/') $dir .= '/';
593
594    do
595    {
596        $path = $dir.$prefix.mt_rand(0, 9999999);
597    } while (!mkdir($path, $mode));
598
599    return $path;
600}
601
602/**
603 * Strips file extension
604 *
605 * @param string $name
606 * @return string
607 */
608function strip_ext($name)
609{
610    $ext = strrchr($name, '.');
611    if($ext !== false)
612    {
613        $name = substr($name, 0, -strlen($ext));
614    }
615    return $name;
616}
617
618?>