speedealing /htdocs/viewimage.php

Language PHP Lines 417
MD5 Hash 14f12363a9122752c543b469703b5c11 Estimated Cost $7,072 (why?)
Repository https://bitbucket.org/speedealing/speedealing.git View Raw File View Project SPDX
  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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
<?php
/* Copyright (C) 2004-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
 * Copyright (C) 2005-2012 Laurent Destailleur  <eldy@users.sourceforge.net>
 * Copyright (C) 2005-2012 Regis Houssin        <regis.houssin@capnetworks.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 * or see http://www.gnu.org/
 */

/**
 *		\file       htdocs/viewimage.php
 *		\brief      Wrapper to show images into Speedealing screens
 *      \remarks    Call to wrapper is '<img src="'.DOL_URL_ROOT.'/viewimage.php?modulepart=diroffile&file=relativepathofofile&cache=0">'
 */

//if (! defined('NOREQUIREUSER'))   define('NOREQUIREUSER','1');	// Not disabled cause need to load personalized language
//if (! defined('NOREQUIREDB'))   define('NOREQUIREDB','1');		// Not disabled cause need to load personalized language
if (! defined('NOREQUIRESOC'))    define('NOREQUIRESOC','1');
if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN','1');
if (! defined('NOCSRFCHECK'))     define('NOCSRFCHECK','1');
if (! defined('NOTOKENRENEWAL'))  define('NOTOKENRENEWAL','1');
if (! defined('NOREQUIREMENU'))  define('NOREQUIREMENU','1');
if (! defined('NOREQUIREHTML'))  define('NOREQUIREHTML','1');
if (! defined('NOREQUIREAJAX'))  define('NOREQUIREAJAX','1');
// Pour autre que companylogo, on charge environnement + info issus de logon comme le user
if ((isset($_GET["modulepart"]) && $_GET["modulepart"] == 'companylogo') && ! defined("NOLOGIN")) define("NOLOGIN",'1');


/**
 * Wrapper, donc header vierge
 *
 * @return  null
 */
function llxHeader() { }

require 'main.inc.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';


$action=GETPOST('action','alpha');
$original_file=GETPOST("file");
$modulepart=GETPOST('modulepart','alpha');
$urlsource=GETPOST("urlsource");
$entity=GETPOST('entity','int');
if ($entity == '') $entity=1;    // For backward compatibility

// Security check
if (empty($modulepart)) accessforbidden('Bad value for parameter modulepart');



/*
 * Actions
 */

// None



/*
 * View
 */

if (GETPOST("cache"))
{
    // Important: Following code is to avoid page request by browser and PHP CPU at
    // each Speedealing page access.
    if (empty($dolibarr_nocache))
    {
        header('Cache-Control: max-age=3600, public, must-revalidate');
        header('Pragma: cache');       // This is to avoid having Pragma: no-cache
    }
    else header('Cache-Control: no-cache');
    //print $dolibarr_nocache; exit;
}

// Define mime type
$type = 'application/octet-stream';
if (! empty($_GET["type"])) $type=$_GET["type"];
else $type=dol_mimetype($original_file);

// Suppression de la chaine de caractere ../ dans $original_file
$original_file = str_replace("../","/", $original_file);

// Security checks
if (empty($modulepart)) accessforbidden('Bad value for parameter modulepart');
$accessallowed=0;
if ($modulepart)
{
    // Check permissions and define directory

    // Wrapping for company logo
    if ($modulepart == 'companylogo')
    {
        $accessallowed=1;
        $original_file=$conf->mycompany->dir_output.'/logos/'.$original_file;
    }
    // Wrapping for users photos
    elseif ($modulepart == 'userphoto')
    {
        $accessallowed=1;
        $original_file=$conf->user->dir_output.'/'.$original_file;
    }
    // Wrapping for members photos
    elseif ($modulepart == 'memberphoto')
    {
        $accessallowed=1;
        $original_file=$conf->adherent->dir_output.'/'.$original_file;
    }
    // Wrapping for members photos
    elseif ($modulepart == 'member')
    {
        $accessallowed=1;
        $original_file=$conf->adherent->dir_output.'/'.$original_file;
    }
    // Wrapping pour les images des societes
    elseif ($modulepart == 'societe')
    {
        $accessallowed=1;
        $original_file=$conf->societe->multidir_output[$entity].'/'.$original_file;
    }
    // Wrapping pour les apercu factures
    elseif ($modulepart == 'apercufacture')
    {
        if ($user->rights->facture->lire) $accessallowed=1;
        $original_file=$conf->facture->dir_output.'/'.$original_file;
    }
    // Wrapping pour les apercu propal
    elseif ($modulepart == 'apercupropal')
    {
        if ($user->rights->propale->lire) $accessallowed=1;
        $original_file=$conf->propal->dir_output.'/'.$original_file;
    }
    // Wrapping pour les apercu commande
    elseif ($modulepart == 'apercucommande')
    {
        if ($user->rights->commande->lire) $accessallowed=1;
        $original_file=$conf->commande->dir_output.'/'.$original_file;
    }
    // Wrapping pour les apercu intervention
    elseif ($modulepart == 'apercufichinter')
    {
        if ($user->rights->ficheinter->lire) $accessallowed=1;
        $original_file=$conf->ficheinter->dir_output.'/'.$original_file;
    }
    // Wrapping pour les images des stats propales
    elseif ($modulepart == 'propalstats')
    {
        if ($user->rights->propale->lire) $accessallowed=1;
        $original_file=$conf->propal->dir_temp.'/'.$original_file;
    }
    // Wrapping pour les images des stats commandes
    elseif ($modulepart == 'orderstats')
    {
        if ($user->rights->commande->lire) $accessallowed=1;
        $original_file=$conf->commande->dir_temp.'/'.$original_file;
    }
    elseif ($modulepart == 'orderstatssupplier')
    {
        if ($user->rights->fournisseur->commande->lire) $accessallowed=1;
        $original_file=$conf->fournisseur->dir_output.'/commande/temp/'.$original_file;
    }
    // Wrapping pour les images des stats factures
    elseif ($modulepart == 'billstats')
    {
        if ($user->rights->facture->lire) $accessallowed=1;
        $original_file=$conf->facture->dir_temp.'/'.$original_file;
    }
    elseif ($modulepart == 'billstatssupplier')
    {
        if ($user->rights->fournisseur->facture->lire) $accessallowed=1;
        $original_file=$conf->fournisseur->dir_output.'/facture/temp/'.$original_file;
    }
    // Wrapping pour les images des stats expeditions
    elseif ($modulepart == 'expeditionstats')
    {
        if ($user->rights->expedition->lire) $accessallowed=1;
        $original_file=$conf->expedition->dir_temp.'/'.$original_file;
    }
    // Wrapping pour les images des stats expeditions
    elseif ($modulepart == 'tripsexpensesstats')
    {
        if ($user->rights->deplacement->lire) $accessallowed=1;
        $original_file=$conf->deplacement->dir_temp.'/'.$original_file;
    }
    // Wrapping pour les images des stats expeditions
    elseif ($modulepart == 'memberstats')
    {
        if ($user->rights->adherent->lire) $accessallowed=1;
        $original_file=$conf->adherent->dir_temp.'/'.$original_file;
    }
    // Wrapping pour les images des stats produits
    elseif (preg_match('/^productstats_/i',$modulepart))
    {
        if ($user->rights->produit->lire || $user->rights->service->lire) $accessallowed=1;
        $original_file=(!empty($conf->product->multidir_temp[$entity])?$conf->product->multidir_temp[$entity]:$conf->service->multidir_temp[$entity]).'/'.$original_file;
    }
    // Wrapping for products or services
    elseif ($modulepart == 'product')
    {
        if ($user->rights->produit->lire || $user->rights->service->lire) $accessallowed=1;
        $original_file=(! empty($conf->product->multidir_output[$entity])?$conf->product->multidir_output[$entity]:$conf->service->multidir_output[$entity]).'/'.$original_file;
    }
    // Wrapping for products or services
    elseif ($modulepart == 'tax')
    {
        if ($user->rights->tax->charges->lire) $accessallowed=1;
        $original_file=$conf->tax->dir_output.'/'.$original_file;
    }
    // Wrapping for products or services
    elseif ($modulepart == 'actions')
    {
        if ($user->rights->agenda->myactions->read) $accessallowed=1;
        $original_file=$conf->agenda->dir_output.'/'.$original_file;
    }
    // Wrapping for categories
    elseif ($modulepart == 'category')
    {
        if ($user->rights->categorie->lire) $accessallowed=1;
        $original_file=$conf->categorie->multidir_output[$entity].'/'.$original_file;
    }
    // Wrapping pour les prelevements
    elseif ($modulepart == 'prelevement')
    {
        if ($user->rights->prelevement->bons->lire) $accessallowed=1;
        $original_file=$conf->prelevement->dir_output.'/receipts/'.$original_file;
    }
    // Wrapping pour les graph energie
    elseif ($modulepart == 'graph_stock')
    {
        $accessallowed=1;
        $original_file=$conf->stock->dir_temp.'/'.$original_file;
    }
    // Wrapping pour les graph fournisseurs
    elseif ($modulepart == 'graph_fourn')
    {
        $accessallowed=1;
        $original_file=$conf->fournisseur->dir_temp.'/'.$original_file;
    }
    // Wrapping pour les graph des produits
    elseif ($modulepart == 'graph_product')
    {
        $accessallowed=1;
        $original_file=$conf->product->multidir_temp[$entity].'/'.$original_file;
    }
    // Wrapping pour les code barre
    elseif ($modulepart == 'barcode')
    {
        $accessallowed=1;
        // If viewimage is called for barcode, we try to output an image on the fly,
        // with not build of file on disk.
        //$original_file=$conf->barcode->dir_temp.'/'.$original_file;
        $original_file='';
    }
    // Wrapping pour les icones de background des mailings
    elseif ($modulepart == 'iconmailing')
    {
        $accessallowed=1;
        $original_file=$conf->mailing->dir_temp.'/'.$original_file;
    }
    // Wrapping pour les icones de background des mailings
    elseif ($modulepart == 'scanner_user_temp')
    {
        $accessallowed=1;
        $original_file=$conf->scanner->dir_temp.'/'.$user->id.'/'.$original_file;
    }
    // Wrapping pour les images fckeditor
    elseif ($modulepart == 'fckeditor')
    {
        $accessallowed=1;
        $original_file=$conf->fckeditor->dir_output.'/'.$original_file;
    }

    // GENERIC Wrapping
    // If modulepart=module_user_temp	Allows any module to open a file if file is in directory called DOL_DATA_ROOT/modulepart/temp/iduser
    // If modulepart=module_temp		Allows any module to open a file if file is in directory called DOL_DATA_ROOT/modulepart/temp
    // If modulepart=module_user		Allows any module to open a file if file is in directory called DOL_DATA_ROOT/modulepart/iduser
    // If modulepart=module				Allows any module to open a file if file is in directory called DOL_DATA_ROOT/modulepart
    else
    {
        if (preg_match('/^([a-z]+)_user_temp$/i',$modulepart,$reg))
        {
            if ($user->rights->$reg[1]->lire || $user->rights->$reg[1]->read) $accessallowed=1;
            $original_file=$conf->$reg[1]->dir_temp.'/'.$user->id.'/'.$original_file;
        }
        else if (preg_match('/^([a-z]+)_temp$/i',$modulepart,$reg))
        {
            if ($user->rights->$reg[1]->lire || $user->rights->$reg[1]->read) $accessallowed=1;
            $original_file=$conf->$reg[1]->dir_temp.'/'.$original_file;
        }
        else if (preg_match('/^([a-z]+)_user$/i',$modulepart,$reg))
        {
            if ($user->rights->$reg[1]->lire || $user->rights->$reg[1]->read) $accessallowed=1;
            $original_file=$conf->$reg[1]->dir_output.'/'.$user->id.'/'.$original_file;
        }
        else
        {
            $perm=GETPOST('perm');
            $subperm=GETPOST('subperm');
            if ($perm || $subperm)
            {
                if (($perm && ! $subperm && $user->rights->$modulepart->$perm) || ($perm && $subperm && $user->rights->$modulepart->$perm->$subperm)) $accessallowed=1;
                $original_file=$conf->$modulepart->dir_output.'/'.$original_file;
            }
            else
            {
                if ($user->rights->$modulepart->lire || $user->rights->$modulepart->read) $accessallowed=1;
                $original_file=$conf->$modulepart->dir_output.'/'.$original_file;
            }
        }
    }
}

// Security:
// Limit access if permissions are wrong
if (! $accessallowed)
{
    accessforbidden();
}

// Security:
// On interdit les remontees de repertoire ainsi que les pipe dans les noms de fichiers.
if (preg_match('/\.\./',$original_file) || preg_match('/[<>|]/',$original_file))
{
    dol_syslog("Refused to deliver file ".$original_file, LOG_WARNING);
    // Do no show plain path in shown error message
    dol_print_error(0,'Error: File '.$_GET["file"].' does not exists');
    exit;
}



if ($modulepart == 'barcode')
{
    $generator=GETPOST("generator","alpha");
    $code=GETPOST("code");
    $encoding=GETPOST("encoding","alpha");
    $readable=GETPOST("readable")?GETPOST("readable","alpha"):"Y";

    if (empty($generator) || empty($encoding))
    {
        dol_print_error(0,'Error, parameter "generator" or "encoding" not defined');
        exit;
    }

    $dirbarcode=array_merge(array("/core/modules/barcode/"),$conf->modules_parts['barcode']);

    $result=0;

    foreach($dirbarcode as $reldir)
    {
        $dir=dol_buildpath($reldir,0);
        $newdir=dol_osencode($dir);

        // Check if directory exists (we do not use dol_is_dir to avoid loading files.lib.php)
        if (! is_dir($newdir)) continue;

        $result=@include_once $newdir.$generator.'.modules.php';
        if ($result) break;
    }

    // Load barcode class
    $classname = "mod".ucfirst($generator);
    $module = new $classname($db);
    if ($module->encodingIsSupported($encoding))
    {
        $result=$module->buildBarCode($code,$encoding,$readable);
    }
}
else					// Open and return file
{
    clearstatcache();

    // Output files on browser
    dol_syslog("viewimage.php return file $original_file content-type=$type");

    // This test is to avoid error images when image is not available (for example thumbs).
    if (! dol_is_file($original_file))
    {
        $original_file=DOL_DOCUMENT_ROOT.'/theme/common/nophoto.jpg';
        /*$error='Error: File '.$_GET["file"].' does not exists or filesystems permissions are not allowed';
        dol_print_error(0,$error);
        print $error;
        exit;*/
    }

    // Les drois sont ok et fichier trouve
    if ($type)
    {
        header('Content-Disposition: inline; filename="'.basename($original_file).'"');
        header('Content-type: '.$type);
    }
    else
    {
        header('Content-Disposition: inline; filename="'.basename($original_file).'"');
        header('Content-type: image/png');
    }

    $original_file_osencoded=dol_osencode($original_file);
    readfile($original_file_osencoded);
}


if (is_object($db)) $db->close();
?>
Back to Top