xref: /dokuwiki/inc/media.php (revision bf1f3ac4f72c26ff386c08c920dda651d91c6898)
13df72098SAndreas Gohr<?php
23df72098SAndreas Gohr/**
33df72098SAndreas Gohr * All output and handler function needed for the media management popup
43df72098SAndreas Gohr *
53df72098SAndreas Gohr * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
63df72098SAndreas Gohr * @author     Andreas Gohr <andi@splitbrain.org>
73df72098SAndreas Gohr */
83df72098SAndreas Gohr
9fa8adffeSAndreas Gohrif(!defined('DOKU_INC')) die('meh.');
103df72098SAndreas Gohrif(!defined('NL')) define('NL',"\n");
113df72098SAndreas Gohrrequire_once(DOKU_INC.'inc/html.php');
123df72098SAndreas Gohrrequire_once(DOKU_INC.'inc/search.php');
133df72098SAndreas Gohrrequire_once(DOKU_INC.'inc/JpegMeta.php');
143df72098SAndreas Gohr
153df72098SAndreas Gohr/**
163df72098SAndreas Gohr * Lists pages which currently use a media file selected for deletion
173df72098SAndreas Gohr *
183df72098SAndreas Gohr * References uses the same visual as search results and share
193df72098SAndreas Gohr * their CSS tags except pagenames won't be links.
203df72098SAndreas Gohr *
213df72098SAndreas Gohr * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
223df72098SAndreas Gohr */
233df72098SAndreas Gohrfunction media_filesinuse($data,$id){
243df72098SAndreas Gohr    global $lang;
253df72098SAndreas Gohr    echo '<h1>'.$lang['reference'].' <code>'.hsc(noNS($id)).'</code></h1>';
263df72098SAndreas Gohr    echo '<p>'.hsc($lang['ref_inuse']).'</p>';
273df72098SAndreas Gohr
283df72098SAndreas Gohr    $hidden=0; //count of hits without read permission
293df72098SAndreas Gohr    foreach($data as $row){
30a05e297aSAndreas Gohr        if(auth_quickaclcheck($row) >= AUTH_READ && isVisiblePage($row)){
313df72098SAndreas Gohr            echo '<div class="search_result">';
32a05e297aSAndreas Gohr            echo '<span class="mediaref_ref">'.hsc($row).'</span>';
333df72098SAndreas Gohr            echo '</div>';
343df72098SAndreas Gohr        }else
353df72098SAndreas Gohr        $hidden++;
363df72098SAndreas Gohr    }
373df72098SAndreas Gohr    if ($hidden){
383df72098SAndreas Gohr      print '<div class="mediaref_hidden">'.$lang['ref_hidden'].'</div>';
393df72098SAndreas Gohr    }
403df72098SAndreas Gohr}
413df72098SAndreas Gohr
423df72098SAndreas Gohr/**
433df72098SAndreas Gohr * Handles the saving of image meta data
443df72098SAndreas Gohr *
453df72098SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
463df72098SAndreas Gohr */
473df72098SAndreas Gohrfunction media_metasave($id,$auth,$data){
483df72098SAndreas Gohr    if($auth < AUTH_UPLOAD) return false;
49f2ea8432SAndreas Gohr    if(!checkSecurityToken()) return false;
503df72098SAndreas Gohr    global $lang;
510b308644SOtto Vainio    global $conf;
523df72098SAndreas Gohr    $src = mediaFN($id);
533df72098SAndreas Gohr
543df72098SAndreas Gohr    $meta = new JpegMeta($src);
553df72098SAndreas Gohr    $meta->_parseAll();
563df72098SAndreas Gohr
573df72098SAndreas Gohr    foreach($data as $key => $val){
583df72098SAndreas Gohr        $val=trim($val);
593df72098SAndreas Gohr        if(empty($val)){
603df72098SAndreas Gohr            $meta->deleteField($key);
613df72098SAndreas Gohr        }else{
623df72098SAndreas Gohr            $meta->setField($key,$val);
633df72098SAndreas Gohr        }
643df72098SAndreas Gohr    }
653df72098SAndreas Gohr
663df72098SAndreas Gohr    if($meta->save()){
670b308644SOtto Vainio        if($conf['fperm']) chmod($src, $conf['fperm']);
683df72098SAndreas Gohr        msg($lang['metasaveok'],1);
693df72098SAndreas Gohr        return $id;
703df72098SAndreas Gohr    }else{
713df72098SAndreas Gohr        msg($lang['metasaveerr'],-1);
723df72098SAndreas Gohr        return false;
733df72098SAndreas Gohr    }
743df72098SAndreas Gohr}
753df72098SAndreas Gohr
763df72098SAndreas Gohr/**
773df72098SAndreas Gohr * Display the form to edit image meta data
783df72098SAndreas Gohr *
793df72098SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
803df72098SAndreas Gohr */
813df72098SAndreas Gohrfunction media_metaform($id,$auth){
823df72098SAndreas Gohr    if($auth < AUTH_UPLOAD) return false;
83f8121585SChris Smith    global $lang, $config_cascade;
843df72098SAndreas Gohr
853df72098SAndreas Gohr    // load the field descriptions
863df72098SAndreas Gohr    static $fields = null;
873df72098SAndreas Gohr    if(is_null($fields)){
88f8121585SChris Smith
89f8121585SChris Smith      foreach (array('default','local') as $config_group) {
90f8121585SChris Smith        if (empty($config_cascade['mediameta'][$config_group])) continue;
91f8121585SChris Smith        foreach ($config_cascade['mediameta'][$config_group] as $config_file) {
92f8121585SChris Smith          if(@file_exists($config_file)){
93f8121585SChris Smith            include($config_file);
94d578eae7SAndreas Gohr          }
95f8121585SChris Smith        }
963df72098SAndreas Gohr      }
973df72098SAndreas Gohr    }
983df72098SAndreas Gohr
993df72098SAndreas Gohr    $src = mediaFN($id);
1003df72098SAndreas Gohr
1013df72098SAndreas Gohr    // output
1023df72098SAndreas Gohr    echo '<h1>'.hsc(noNS($id)).'</h1>'.NL;
1033df72098SAndreas Gohr    echo '<form action="'.DOKU_BASE.'lib/exe/mediamanager.php" accept-charset="utf-8" method="post" class="meta">'.NL;
104f2ea8432SAndreas Gohr    formSecurityToken();
1053df72098SAndreas Gohr    foreach($fields as $key => $field){
1063df72098SAndreas Gohr        // get current value
1073df72098SAndreas Gohr        $tags = array($field[0]);
1083df72098SAndreas Gohr        if(is_array($field[3])) $tags = array_merge($tags,$field[3]);
1093df72098SAndreas Gohr        $value = tpl_img_getTag($tags,'',$src);
110ca6a0701SAndreas Gohr        $value = cleanText($value);
1113df72098SAndreas Gohr
1123df72098SAndreas Gohr        // prepare attributes
1133df72098SAndreas Gohr        $p = array();
1143df72098SAndreas Gohr        $p['class'] = 'edit';
1153df72098SAndreas Gohr        $p['id']    = 'meta__'.$key;
1163df72098SAndreas Gohr        $p['name']  = 'meta['.$field[0].']';
1173df72098SAndreas Gohr
1183df72098SAndreas Gohr        // put label
1193df72098SAndreas Gohr        echo '<div class="metafield">';
1203df72098SAndreas Gohr        echo '<label for="meta__'.$key.'">';
1213df72098SAndreas Gohr        echo ($lang[$field[1]]) ? $lang[$field[1]] : $field[1];
122cf6894dfSAndreas Gohr        echo ':</label>';
1233df72098SAndreas Gohr
1243df72098SAndreas Gohr        // put input field
1253df72098SAndreas Gohr        if($field[2] == 'text'){
1263df72098SAndreas Gohr            $p['value'] = $value;
1273df72098SAndreas Gohr            $p['type']  = 'text';
1283df72098SAndreas Gohr            $att = buildAttributes($p);
1293df72098SAndreas Gohr            echo "<input $att/>".NL;
1303df72098SAndreas Gohr        }else{
1313df72098SAndreas Gohr            $att = buildAttributes($p);
1321440e523SAnika Henke            echo "<textarea $att rows=\"6\" cols=\"50\">".formText($value).'</textarea>'.NL;
1333df72098SAndreas Gohr        }
1343df72098SAndreas Gohr        echo '</div>'.NL;
1353df72098SAndreas Gohr    }
1363df72098SAndreas Gohr    echo '<div class="buttons">'.NL;
1371440e523SAnika Henke    echo '<input type="hidden" name="img" value="'.hsc($id).'" />'.NL;
1383df72098SAndreas Gohr    echo '<input name="do[save]" type="submit" value="'.$lang['btn_save'].
13907493d05SAnika Henke         '" title="'.$lang['btn_save'].' [S]" accesskey="s" class="button" />'.NL;
1403df72098SAndreas Gohr    echo '<input name="do[cancel]" type="submit" value="'.$lang['btn_cancel'].
14107493d05SAnika Henke         '" title="'.$lang['btn_cancel'].' [C]" accesskey="c" class="button" />'.NL;
1424868e1c6SAndreas Gohr    echo '</div>'.NL;
1433df72098SAndreas Gohr    echo '</form>'.NL;
1443df72098SAndreas Gohr}
1453df72098SAndreas Gohr
146666cdec5SMichael Klier/**
147666cdec5SMichael Klier * Conveinience function to check if a media file is still in use
148666cdec5SMichael Klier *
149666cdec5SMichael Klier * @author Michael Klier <chi@chimeric.de>
150666cdec5SMichael Klier */
151666cdec5SMichael Klierfunction media_inuse($id) {
152666cdec5SMichael Klier    global $conf;
153666cdec5SMichael Klier    $mediareferences = array();
154666cdec5SMichael Klier    if($conf['refcheck']){
155666cdec5SMichael Klier        require_once(DOKU_INC.'inc/fulltext.php');
156666cdec5SMichael Klier        $mediareferences = ft_mediause($id,$conf['refshow']);
157666cdec5SMichael Klier        if(!count($mediareferences)) {
1586dae2464SAndreas Gohr            return false;
159666cdec5SMichael Klier        } else {
160666cdec5SMichael Klier            return $mediareferences;
161666cdec5SMichael Klier        }
162666cdec5SMichael Klier    } else {
163666cdec5SMichael Klier        return false;
164666cdec5SMichael Klier    }
165666cdec5SMichael Klier}
166a05e297aSAndreas Gohr
1673df72098SAndreas Gohr/**
1683df72098SAndreas Gohr * Handles media file deletions
1693df72098SAndreas Gohr *
1703df72098SAndreas Gohr * If configured, checks for media references before deletion
1713df72098SAndreas Gohr *
1723df72098SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
1733df72098SAndreas Gohr * @return mixed false on error, true on delete or array with refs
1743df72098SAndreas Gohr */
1753df72098SAndreas Gohrfunction media_delete($id,$auth){
1763df72098SAndreas Gohr    if($auth < AUTH_DELETE) return false;
177f2ea8432SAndreas Gohr    if(!checkSecurityToken()) return false;
1783df72098SAndreas Gohr    global $conf;
1793df72098SAndreas Gohr    global $lang;
1803df72098SAndreas Gohr
1813df72098SAndreas Gohr    $file = mediaFN($id);
1824a961e72SMichal Kolodziejski
1834a961e72SMichal Kolodziejski    // trigger an event - MEDIA_DELETE_FILE
184666cdec5SMichael Klier    $data['id']   = $id;
1854a961e72SMichal Kolodziejski    $data['name'] = basename($file);
1864a961e72SMichal Kolodziejski    $data['path'] = $file;
1874a961e72SMichal Kolodziejski    $data['size'] = (@file_exists($file)) ? filesize($file) : 0;
188666cdec5SMichael Klier
189666cdec5SMichael Klier    $data['unl'] = false;
190666cdec5SMichael Klier    $data['del'] = false;
1914a961e72SMichal Kolodziejski    $evt = new Doku_Event('MEDIA_DELETE_FILE',$data);
1924a961e72SMichal Kolodziejski    if ($evt->advise_before()) {
193666cdec5SMichael Klier        $data['unl'] = @unlink($file);
194666cdec5SMichael Klier        if($data['unl']){
19599c8d7f2Smichael            addMediaLogEntry(time(), $id, DOKU_CHANGE_TYPE_DELETE);
196666cdec5SMichael Klier            $data['del'] = io_sweepNS($id,'mediadir');
1973df72098SAndreas Gohr        }
1984a961e72SMichal Kolodziejski    }
1994a961e72SMichal Kolodziejski    $evt->advise_after();
2004a961e72SMichal Kolodziejski    unset($evt);
2014a961e72SMichal Kolodziejski
202666cdec5SMichael Klier    if($data['unl'] && $data['del']){
203666cdec5SMichael Klier        // current namespace was removed. redirecting to root ns passing msg along
204af2408d5SAndreas Gohr        send_redirect(DOKU_URL.'lib/exe/mediamanager.php?msg1='.
205365be586SAndreas Gohr                rawurlencode(sprintf(noNS($id),$lang['deletesucc'])));
2063df72098SAndreas Gohr    }
2073df72098SAndreas Gohr
208666cdec5SMichael Klier    return $data['unl'];
2093df72098SAndreas Gohr}
2103df72098SAndreas Gohr
2113df72098SAndreas Gohr/**
2123df72098SAndreas Gohr * Handles media file uploads
2133df72098SAndreas Gohr *
21411d9dfa5SMichael Klier * This generates an action event and delegates to _media_upload_action().
21511d9dfa5SMichael Klier * Action plugins are allowed to pre/postprocess the uploaded file.
21611d9dfa5SMichael Klier * (The triggered event is preventable.)
21711d9dfa5SMichael Klier *
21811d9dfa5SMichael Klier * Event data:
21911d9dfa5SMichael Klier * $data[0]     fn_tmp: the temporary file name (read from $_FILES)
22011d9dfa5SMichael Klier * $data[1]     fn: the file name of the uploaded file
22111d9dfa5SMichael Klier * $data[2]     id: the future directory id of the uploaded file
22211d9dfa5SMichael Klier * $data[3]     imime: the mimetype of the uploaded file
22399c8d7f2Smichael * $data[4]     overwrite: if an existing file is going to be overwritten
22411d9dfa5SMichael Klier *
22511d9dfa5SMichael Klier * @triggers MEDIA_UPLOAD_FINISH
2263df72098SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
22711d9dfa5SMichael Klier * @author Michael Klier <chi@chimeric.de>
2283df72098SAndreas Gohr * @return mixed false on error, id of the new file on success
2293df72098SAndreas Gohr */
2303df72098SAndreas Gohrfunction media_upload($ns,$auth){
2313df72098SAndreas Gohr    if($auth < AUTH_UPLOAD) return false;
232f2ea8432SAndreas Gohr    if(!checkSecurityToken()) return false;
2333df72098SAndreas Gohr    require_once(DOKU_INC.'inc/confutils.php');
2343df72098SAndreas Gohr    global $lang;
2353df72098SAndreas Gohr    global $conf;
2363df72098SAndreas Gohr
23744409c3dSAndreas Gohr    // get file and id
2383df72098SAndreas Gohr    $id   = $_POST['id'];
2393df72098SAndreas Gohr    $file = $_FILES['upload'];
2403df72098SAndreas Gohr    if(empty($id)) $id = $file['name'];
24144409c3dSAndreas Gohr
2429676dc23SAndreas Gohr    // check for data
2439676dc23SAndreas Gohr    if(!@filesize($file['tmp_name'])){
2449676dc23SAndreas Gohr        msg('No data uploaded. Disk full?',-1);
2459676dc23SAndreas Gohr        return false;
2469676dc23SAndreas Gohr    }
2479676dc23SAndreas Gohr
24844409c3dSAndreas Gohr    // check extensions
249ecebf3a8SAndreas Gohr    list($fext,$fmime,$dl) = mimetype($file['name']);
250ecebf3a8SAndreas Gohr    list($iext,$imime,$dl) = mimetype($id);
25144409c3dSAndreas Gohr    if($fext && !$iext){
2528cb1eb01SAndreas Gohr        // no extension specified in id - read original one
25344409c3dSAndreas Gohr        $id   .= '.'.$fext;
2548cb1eb01SAndreas Gohr        $imime = $fmime;
25544409c3dSAndreas Gohr    }elseif($fext && $fext != $iext){
25644409c3dSAndreas Gohr        // extension was changed, print warning
25744409c3dSAndreas Gohr        msg(sprintf($lang['mediaextchange'],$fext,$iext));
25844409c3dSAndreas Gohr    }
25944409c3dSAndreas Gohr
2603df72098SAndreas Gohr    // get filename
26163b0c1a7SGina Haeussge    $id   = cleanID($ns.':'.$id,false,true);
2623df72098SAndreas Gohr    $fn   = mediaFN($id);
2633df72098SAndreas Gohr
2643df72098SAndreas Gohr    // get filetype regexp
2653df72098SAndreas Gohr    $types = array_keys(getMimeTypes());
2663df72098SAndreas Gohr    $types = array_map(create_function('$q','return preg_quote($q,"/");'),$types);
2673df72098SAndreas Gohr    $regex = join('|',$types);
2683df72098SAndreas Gohr
2693df72098SAndreas Gohr    // because a temp file was created already
2703df72098SAndreas Gohr    if(preg_match('/\.('.$regex.')$/i',$fn)){
2713df72098SAndreas Gohr        //check for overwrite
27299c8d7f2Smichael        $overwrite = @file_exists($fn);
27399c8d7f2Smichael        if($overwrite && (!$_REQUEST['ow'] || $auth < AUTH_DELETE)){
2743df72098SAndreas Gohr            msg($lang['uploadexist'],0);
2753df72098SAndreas Gohr            return false;
2763df72098SAndreas Gohr        }
2778cb1eb01SAndreas Gohr        // check for valid content
2788cb1eb01SAndreas Gohr        $ok = media_contentcheck($file['tmp_name'],$imime);
2798cb1eb01SAndreas Gohr        if($ok == -1){
2808cb1eb01SAndreas Gohr            msg(sprintf($lang['uploadbadcontent'],".$iext"),-1);
2818cb1eb01SAndreas Gohr            return false;
2828cb1eb01SAndreas Gohr        }elseif($ok == -2){
2838cb1eb01SAndreas Gohr            msg($lang['uploadspam'],-1);
2848cb1eb01SAndreas Gohr            return false;
28526ceae18SAndreas Gohr        }elseif($ok == -3){
28626ceae18SAndreas Gohr            msg($lang['uploadxss'],-1);
28726ceae18SAndreas Gohr            return false;
2888cb1eb01SAndreas Gohr        }
2898cb1eb01SAndreas Gohr
29011d9dfa5SMichael Klier        // prepare event data
29111d9dfa5SMichael Klier        $data[0] = $file['tmp_name'];
29211d9dfa5SMichael Klier        $data[1] = $fn;
29311d9dfa5SMichael Klier        $data[2] = $id;
29411d9dfa5SMichael Klier        $data[3] = $imime;
29599c8d7f2Smichael        $data[4] = $overwrite;
29611d9dfa5SMichael Klier
29711d9dfa5SMichael Klier        // trigger event
29811d9dfa5SMichael Klier        return trigger_event('MEDIA_UPLOAD_FINISH', $data, '_media_upload_action', true);
29911d9dfa5SMichael Klier
30011d9dfa5SMichael Klier    }else{
30111d9dfa5SMichael Klier        msg($lang['uploadwrong'],-1);
30211d9dfa5SMichael Klier    }
30311d9dfa5SMichael Klier    return false;
30411d9dfa5SMichael Klier}
30511d9dfa5SMichael Klier
30611d9dfa5SMichael Klier/**
30711d9dfa5SMichael Klier * Callback adapter for media_upload_finish()
30811d9dfa5SMichael Klier * @author Michael Klier <chi@chimeric.de>
30911d9dfa5SMichael Klier */
31011d9dfa5SMichael Klierfunction _media_upload_action($data) {
31111d9dfa5SMichael Klier    // fixme do further sanity tests of given data?
31299c8d7f2Smichael    if(is_array($data) && count($data)===5) {
31399c8d7f2Smichael        return media_upload_finish($data[0], $data[1], $data[2], $data[3], $data[4]);
31411d9dfa5SMichael Klier    } else {
31511d9dfa5SMichael Klier        return false; //callback error
31611d9dfa5SMichael Klier    }
31711d9dfa5SMichael Klier}
31811d9dfa5SMichael Klier
31911d9dfa5SMichael Klier/**
32011d9dfa5SMichael Klier * Saves an uploaded media file
32111d9dfa5SMichael Klier *
32211d9dfa5SMichael Klier * @author Andreas Gohr <andi@splitbrain.org>
32311d9dfa5SMichael Klier * @author Michael Klier <chi@chimeric.de>
32411d9dfa5SMichael Klier */
32599c8d7f2Smichaelfunction media_upload_finish($fn_tmp, $fn, $id, $imime, $overwrite) {
32611d9dfa5SMichael Klier    global $conf;
32711d9dfa5SMichael Klier    global $lang;
32811d9dfa5SMichael Klier
3293df72098SAndreas Gohr    // prepare directory
330cc7d0c94SBen Coburn    io_createNamespace($id, 'media');
33111d9dfa5SMichael Klier
33211d9dfa5SMichael Klier    if(move_uploaded_file($fn_tmp, $fn)) {
33374400ea5SBen Coburn        // Set the correct permission here.
33474400ea5SBen Coburn        // Always chmod media because they may be saved with different permissions than expected from the php umask.
33574400ea5SBen Coburn        // (Should normally chmod to $conf['fperm'] only if $conf['fperm'] is set.)
33674400ea5SBen Coburn        chmod($fn, $conf['fmode']);
3373df72098SAndreas Gohr        msg($lang['uploadsucc'],1);
33875030359SAndreas Gohr        media_notify($id,$fn,$imime);
33999c8d7f2Smichael        // add a log entry to the media changelog
34099c8d7f2Smichael        if ($overwrite) {
34199c8d7f2Smichael            addMediaLogEntry(time(), $id, DOKU_CHANGE_TYPE_EDIT);
34299c8d7f2Smichael        } else {
34399c8d7f2Smichael            addMediaLogEntry(time(), $id, DOKU_CHANGE_TYPE_CREATE);
34499c8d7f2Smichael        }
3453df72098SAndreas Gohr        return $id;
3463df72098SAndreas Gohr    }else{
3473df72098SAndreas Gohr        msg($lang['uploadfail'],-1);
3483df72098SAndreas Gohr    }
3493df72098SAndreas Gohr}
3503df72098SAndreas Gohr
3518cb1eb01SAndreas Gohr/**
3528cb1eb01SAndreas Gohr * This function checks if the uploaded content is really what the
35326ceae18SAndreas Gohr * mimetype says it is. We also do spam checking for text types here.
3548cb1eb01SAndreas Gohr *
3558cb1eb01SAndreas Gohr * We need to do this stuff because we can not rely on the browser
3568cb1eb01SAndreas Gohr * to do this check correctly. Yes, IE is broken as usual.
3578cb1eb01SAndreas Gohr *
3588cb1eb01SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
35926ceae18SAndreas Gohr * @link   http://www.splitbrain.org/blog/2007-02/12-internet_explorer_facilitates_cross_site_scripting
3608cb1eb01SAndreas Gohr * @fixme  check all 26 magic IE filetypes here?
3618cb1eb01SAndreas Gohr */
3628cb1eb01SAndreas Gohrfunction media_contentcheck($file,$mime){
36326ceae18SAndreas Gohr    global $conf;
36426ceae18SAndreas Gohr    if($conf['iexssprotect']){
36526ceae18SAndreas Gohr        $fh = @fopen($file, 'rb');
36626ceae18SAndreas Gohr        if($fh){
36726ceae18SAndreas Gohr            $bytes = fread($fh, 256);
36826ceae18SAndreas Gohr            fclose($fh);
36926ceae18SAndreas Gohr            if(preg_match('/<(script|a|img|html|body|iframe)[\s>]/i',$bytes)){
37026ceae18SAndreas Gohr                return -3;
37126ceae18SAndreas Gohr            }
37226ceae18SAndreas Gohr        }
37326ceae18SAndreas Gohr    }
3748cb1eb01SAndreas Gohr    if(substr($mime,0,6) == 'image/'){
3758cb1eb01SAndreas Gohr        $info = @getimagesize($file);
3768cb1eb01SAndreas Gohr        if($mime == 'image/gif' && $info[2] != 1){
3778cb1eb01SAndreas Gohr            return -1;
3788cb1eb01SAndreas Gohr        }elseif($mime == 'image/jpeg' && $info[2] != 2){
3798cb1eb01SAndreas Gohr            return -1;
3808cb1eb01SAndreas Gohr        }elseif($mime == 'image/png' && $info[2] != 3){
3818cb1eb01SAndreas Gohr            return -1;
3828cb1eb01SAndreas Gohr        }
3838cb1eb01SAndreas Gohr        # fixme maybe check other images types as well
3848cb1eb01SAndreas Gohr    }elseif(substr($mime,0,5) == 'text/'){
3858cb1eb01SAndreas Gohr        global $TEXT;
3868cb1eb01SAndreas Gohr        $TEXT = io_readFile($file);
3878cb1eb01SAndreas Gohr        if(checkwordblock()){
3888cb1eb01SAndreas Gohr            return -2;
3898cb1eb01SAndreas Gohr        }
3908cb1eb01SAndreas Gohr    }
3918cb1eb01SAndreas Gohr    return 0;
3928cb1eb01SAndreas Gohr}
3933df72098SAndreas Gohr
3943df72098SAndreas Gohr/**
39575030359SAndreas Gohr * Send a notify mail on uploads
39675030359SAndreas Gohr *
39775030359SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
39875030359SAndreas Gohr */
39975030359SAndreas Gohrfunction media_notify($id,$file,$mime){
40075030359SAndreas Gohr    global $lang;
40175030359SAndreas Gohr    global $conf;
40275030359SAndreas Gohr    if(empty($conf['notify'])) return; //notify enabled?
40375030359SAndreas Gohr
4042f9daf16SAndreas Gohr    $ip = clientIP();
4052f9daf16SAndreas Gohr
40675030359SAndreas Gohr    $text = rawLocale('uploadmail');
407e656dcd4SAndreas Gohr    $text = str_replace('@DATE@',strftime($conf['dformat']),$text);
40875030359SAndreas Gohr    $text = str_replace('@BROWSER@',$_SERVER['HTTP_USER_AGENT'],$text);
4092f9daf16SAndreas Gohr    $text = str_replace('@IPADDRESS@',$ip,$text);
4102f9daf16SAndreas Gohr    $text = str_replace('@HOSTNAME@',gethostsbyaddrs($ip),$text);
41175030359SAndreas Gohr    $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text);
41275030359SAndreas Gohr    $text = str_replace('@USER@',$_SERVER['REMOTE_USER'],$text);
41375030359SAndreas Gohr    $text = str_replace('@MIME@',$mime,$text);
41455b2b31bSAndreas Gohr    $text = str_replace('@MEDIA@',ml($id,'',true,'&',true),$text);
41575030359SAndreas Gohr    $text = str_replace('@SIZE@',filesize_h(filesize($file)),$text);
41675030359SAndreas Gohr
41775030359SAndreas Gohr    $from = $conf['mailfrom'];
41875030359SAndreas Gohr    $from = str_replace('@USER@',$_SERVER['REMOTE_USER'],$from);
41975030359SAndreas Gohr    $from = str_replace('@NAME@',$INFO['userinfo']['name'],$from);
42075030359SAndreas Gohr    $from = str_replace('@MAIL@',$INFO['userinfo']['mail'],$from);
42175030359SAndreas Gohr
42275030359SAndreas Gohr    $subject = '['.$conf['title'].'] '.$lang['mail_upload'].' '.$id;
42375030359SAndreas Gohr
42475030359SAndreas Gohr    mail_send($conf['notify'],$subject,$text,$from);
42575030359SAndreas Gohr}
42675030359SAndreas Gohr
42775030359SAndreas Gohr/**
4283df72098SAndreas Gohr * List all files in a given Media namespace
4293df72098SAndreas Gohr */
4303df72098SAndreas Gohrfunction media_filelist($ns,$auth=null,$jump=''){
4313df72098SAndreas Gohr    global $conf;
4323df72098SAndreas Gohr    global $lang;
4333df72098SAndreas Gohr    $ns = cleanID($ns);
4343df72098SAndreas Gohr
4353df72098SAndreas Gohr    // check auth our self if not given (needed for ajax calls)
4363df72098SAndreas Gohr    if(is_null($auth)) $auth = auth_quickaclcheck("$ns:*");
4373df72098SAndreas Gohr
4386c48a22eSAndreas Gohr    echo '<h1 id="media__ns">:'.hsc($ns).'</h1>'.NL;
4393df72098SAndreas Gohr
4403df72098SAndreas Gohr    if($auth < AUTH_READ){
4413df72098SAndreas Gohr        // FIXME: print permission warning here instead?
4423df72098SAndreas Gohr        echo '<div class="nothing">'.$lang['nothingfound'].'</div>'.NL;
4433df72098SAndreas Gohr        return;
4443df72098SAndreas Gohr    }
4453df72098SAndreas Gohr
4463df72098SAndreas Gohr    media_uploadform($ns, $auth);
4473df72098SAndreas Gohr
4483df72098SAndreas Gohr    $dir = utf8_encodeFN(str_replace(':','/',$ns));
4493df72098SAndreas Gohr    $data = array();
4501a7f560eSAndreas Gohr    search($data,$conf['mediadir'],'search_media',
4511a7f560eSAndreas Gohr           array('showmsg'=>true,'depth'=>1),$dir);
4523df72098SAndreas Gohr
4533df72098SAndreas Gohr    if(!count($data)){
4543df72098SAndreas Gohr        echo '<div class="nothing">'.$lang['nothingfound'].'</div>'.NL;
4553df72098SAndreas Gohr        return;
4563df72098SAndreas Gohr    }
4573df72098SAndreas Gohr
4583df72098SAndreas Gohr    foreach($data as $item){
4593df72098SAndreas Gohr        media_printfile($item,$auth,$jump);
4603df72098SAndreas Gohr    }
4613df72098SAndreas Gohr}
4623df72098SAndreas Gohr
4633df72098SAndreas Gohr/**
464*bf1f3ac4Ssarnowski * List all files found by the search request
465*bf1f3ac4Ssarnowski */
466*bf1f3ac4Ssarnowskifunction media_searchlist($ns,$auth=null,$jump=''){
467*bf1f3ac4Ssarnowski    global $conf;
468*bf1f3ac4Ssarnowski    global $lang;
469*bf1f3ac4Ssarnowski    $ns = cleanID($ns);
470*bf1f3ac4Ssarnowski
471*bf1f3ac4Ssarnowski    // check auth our self if not given (needed for ajax calls)
472*bf1f3ac4Ssarnowski    if(is_null($auth)) $auth = auth_quickaclcheck("$ns:*");
473*bf1f3ac4Ssarnowski
474*bf1f3ac4Ssarnowski    echo '<h1 id="media__ns">Search</h1>'.NL;
475*bf1f3ac4Ssarnowski
476*bf1f3ac4Ssarnowski    if($auth < AUTH_READ){
477*bf1f3ac4Ssarnowski        // FIXME: print permission warning here instead?
478*bf1f3ac4Ssarnowski        echo '<div class="nothing">'.$lang['nothingfound'].'</div>'.NL;
479*bf1f3ac4Ssarnowski        return;
480*bf1f3ac4Ssarnowski    }
481*bf1f3ac4Ssarnowski
482*bf1f3ac4Ssarnowski    media_searchform($ns, $auth);
483*bf1f3ac4Ssarnowski
484*bf1f3ac4Ssarnowski    if (isset($_REQUEST['key']) && strlen($_REQUEST['key']) > 2) {
485*bf1f3ac4Ssarnowski
486*bf1f3ac4Ssarnowski        if (isset($_REQUEST['key_relative'])) {
487*bf1f3ac4Ssarnowski            $dir = utf8_encodeFN(str_replace(':','/',$ns));
488*bf1f3ac4Ssarnowski        } else {
489*bf1f3ac4Ssarnowski            $dir = '';
490*bf1f3ac4Ssarnowski        }
491*bf1f3ac4Ssarnowski
492*bf1f3ac4Ssarnowski        $data = array();
493*bf1f3ac4Ssarnowski        $evt = new Doku_Event('ACTION_MEDIA_SEARCH', $dir);
494*bf1f3ac4Ssarnowski        if ($evt->advise_before()) {
495*bf1f3ac4Ssarnowski            $pattern = '#'.str_replace('#', '\#', $_REQUEST['key']).'#';
496*bf1f3ac4Ssarnowski            search($data,$conf['mediadir'],'search_media', array('showmsg'=>true,'pattern'=>$pattern),$dir);
497*bf1f3ac4Ssarnowski
498*bf1f3ac4Ssarnowski        }
499*bf1f3ac4Ssarnowski        $evt->advise_after();
500*bf1f3ac4Ssarnowski        unset($evt);
501*bf1f3ac4Ssarnowski
502*bf1f3ac4Ssarnowski        if(!count($data)){
503*bf1f3ac4Ssarnowski            echo '<div class="nothing">'.$lang['nothingfound'].'</div>'.NL;
504*bf1f3ac4Ssarnowski            return;
505*bf1f3ac4Ssarnowski        }
506*bf1f3ac4Ssarnowski
507*bf1f3ac4Ssarnowski        foreach($data as $item){
508*bf1f3ac4Ssarnowski            media_printfile($item,$auth,$jump,$display_namespace=true);
509*bf1f3ac4Ssarnowski        }
510*bf1f3ac4Ssarnowski    }
511*bf1f3ac4Ssarnowski}
512*bf1f3ac4Ssarnowski
513*bf1f3ac4Ssarnowski/**
5143df72098SAndreas Gohr * Print action links for a file depending on filetype
5153df72098SAndreas Gohr * and available permissions
5163df72098SAndreas Gohr *
5173df72098SAndreas Gohr * @todo contains inline javascript
5183df72098SAndreas Gohr */
5193df72098SAndreas Gohrfunction media_fileactions($item,$auth){
5203df72098SAndreas Gohr    global $lang;
5213df72098SAndreas Gohr
522cf6894dfSAndreas Gohr    // view button
523cf6894dfSAndreas Gohr    $link = ml($item['id'],'',true);
524cf6894dfSAndreas Gohr    echo ' <a href="'.$link.'" target="_blank"><img src="'.DOKU_BASE.'lib/images/magnifier.png" '.
525cf6894dfSAndreas Gohr         'alt="'.$lang['mediaview'].'" title="'.$lang['mediaview'].'" class="btn" /></a>';
526cf6894dfSAndreas Gohr
527cf6894dfSAndreas Gohr
528cf6894dfSAndreas Gohr    // no further actions if not writable
5293df72098SAndreas Gohr    if(!$item['writable']) return;
5303df72098SAndreas Gohr
5313df72098SAndreas Gohr    // delete button
5323df72098SAndreas Gohr    if($auth >= AUTH_DELETE){
533f2ea8432SAndreas Gohr        echo ' <a href="'.DOKU_BASE.'lib/exe/mediamanager.php?delete='.rawurlencode($item['id']).
534c55fac47SMichael Klier             '&amp;sectok='.getSecurityToken().'" class="btn_media_delete" title="'.$item['id'].'">'.
5353df72098SAndreas Gohr             '<img src="'.DOKU_BASE.'lib/images/trash.png" alt="'.$lang['btn_delete'].'" '.
5363df72098SAndreas Gohr             'title="'.$lang['btn_delete'].'" class="btn" /></a>';
5373df72098SAndreas Gohr    }
5383df72098SAndreas Gohr
5393df72098SAndreas Gohr    // edit button
5403df72098SAndreas Gohr    if($auth >= AUTH_UPLOAD && $item['isimg'] && $item['meta']->getField('File.Mime') == 'image/jpeg'){
5413df72098SAndreas Gohr        echo ' <a href="'.DOKU_BASE.'lib/exe/mediamanager.php?edit='.rawurlencode($item['id']).'">'.
5423df72098SAndreas Gohr             '<img src="'.DOKU_BASE.'lib/images/pencil.png" alt="'.$lang['metaedit'].'" '.
5433df72098SAndreas Gohr             'title="'.$lang['metaedit'].'" class="btn" /></a>';
5443df72098SAndreas Gohr    }
5453df72098SAndreas Gohr
5463df72098SAndreas Gohr}
5473df72098SAndreas Gohr
5483df72098SAndreas Gohr/**
5493df72098SAndreas Gohr * Formats and prints one file in the list
5503df72098SAndreas Gohr */
551*bf1f3ac4Ssarnowskifunction media_printfile($item,$auth,$jump,$display_namespace=false){
55264c9cfd5SAndreas Gohr    global $lang;
5535e7fa82eSAndreas Gohr    global $conf;
55464c9cfd5SAndreas Gohr
5553df72098SAndreas Gohr    // Prepare zebra coloring
5563df72098SAndreas Gohr    // I always wanted to use this variable name :-D
5573df72098SAndreas Gohr    static $twibble = 1;
5583df72098SAndreas Gohr    $twibble *= -1;
5593df72098SAndreas Gohr    $zebra = ($twibble == -1) ? 'odd' : 'even';
5603df72098SAndreas Gohr
5613df72098SAndreas Gohr    // Automatically jump to recent action
5623df72098SAndreas Gohr    if($jump == $item['id']) {
5633df72098SAndreas Gohr        $jump = ' id="scroll__here" ';
5643df72098SAndreas Gohr    }else{
5653df72098SAndreas Gohr        $jump = '';
5663df72098SAndreas Gohr    }
5673df72098SAndreas Gohr
5683df72098SAndreas Gohr    // Prepare fileicons
569ecebf3a8SAndreas Gohr    list($ext,$mime,$dl) = mimetype($item['file']);
5703df72098SAndreas Gohr    $class = preg_replace('/[^_\-a-z0-9]+/i','_',$ext);
5713df72098SAndreas Gohr    $class = 'select mediafile mf_'.$class;
5723df72098SAndreas Gohr
5733df72098SAndreas Gohr    // Prepare filename
5743df72098SAndreas Gohr    $file = utf8_decodeFN($item['file']);
5753df72098SAndreas Gohr
5763df72098SAndreas Gohr    // Prepare info
5773df72098SAndreas Gohr    $info = '';
5783df72098SAndreas Gohr    if($item['isimg']){
5793df72098SAndreas Gohr        $info .= (int) $item['meta']->getField('File.Width');
5803df72098SAndreas Gohr        $info .= '&#215;';
5813df72098SAndreas Gohr        $info .= (int) $item['meta']->getField('File.Height');
5823df72098SAndreas Gohr        $info .= ' ';
5833df72098SAndreas Gohr    }
584e656dcd4SAndreas Gohr    $info .= '<i>'.strftime($conf['dformat'],$item['mtime']).'</i>';
5855e7fa82eSAndreas Gohr    $info .= ' ';
5863df72098SAndreas Gohr    $info .= filesize_h($item['size']);
5873df72098SAndreas Gohr
5883df72098SAndreas Gohr    // ouput
5893df72098SAndreas Gohr    echo '<div class="'.$zebra.'"'.$jump.'>'.NL;
590*bf1f3ac4Ssarnowski    echo '<a name="h_'.$item['id'].'" class="'.$class.'">'.$file.'</a> ';
5913df72098SAndreas Gohr    echo '<span class="info">('.$info.')</span>'.NL;
5923df72098SAndreas Gohr    media_fileactions($item,$auth);
593f495da32SAnika Henke    echo '<div class="example" id="ex_'.str_replace(':','_',$item['id']).'">';
5940b173dceSAndreas Gohr    echo $lang['mediausage'].' <code>{{:'.$item['id'].'}}</code>';
59564c9cfd5SAndreas Gohr    echo '</div>';
5963df72098SAndreas Gohr    if($item['isimg']) media_printimgdetail($item);
5973df72098SAndreas Gohr    echo '<div class="clearer"></div>'.NL;
5983df72098SAndreas Gohr    echo '</div>'.NL;
5993df72098SAndreas Gohr}
6003df72098SAndreas Gohr
6013df72098SAndreas Gohr/**
6023df72098SAndreas Gohr * Prints a thumbnail and metainfos
6033df72098SAndreas Gohr */
6043df72098SAndreas Gohrfunction media_printimgdetail($item){
6053df72098SAndreas Gohr    // prepare thumbnail
6063df72098SAndreas Gohr    $w = (int) $item['meta']->getField('File.Width');
6073df72098SAndreas Gohr    $h = (int) $item['meta']->getField('File.Height');
6083df72098SAndreas Gohr    if($w>120 || $h>120){
6093df72098SAndreas Gohr        $ratio = $item['meta']->getResizeRatio(120);
6103df72098SAndreas Gohr        $w = floor($w * $ratio);
6113df72098SAndreas Gohr        $h = floor($h * $ratio);
6123df72098SAndreas Gohr    }
6133df72098SAndreas Gohr    $src = ml($item['id'],array('w'=>$w,'h'=>$h));
6143df72098SAndreas Gohr    $p = array();
6153df72098SAndreas Gohr    $p['width']  = $w;
6163df72098SAndreas Gohr    $p['height'] = $h;
6173df72098SAndreas Gohr    $p['alt']    = $item['id'];
6183df72098SAndreas Gohr    $p['class']  = 'thumb';
6193df72098SAndreas Gohr    $att = buildAttributes($p);
6203df72098SAndreas Gohr
6213df72098SAndreas Gohr    // output
6223df72098SAndreas Gohr    echo '<div class="detail">';
6233df72098SAndreas Gohr    echo '<div class="thumb">';
624b166ccbcSAndreas Gohr    echo '<a name="d_:'.$item['id'].'" class="select">';
6253df72098SAndreas Gohr    echo '<img src="'.$src.'" '.$att.' />';
6263df72098SAndreas Gohr    echo '</a>';
6273df72098SAndreas Gohr    echo '</div>';
6283df72098SAndreas Gohr
6293df72098SAndreas Gohr    // read EXIF/IPTC data
63049ac3837Shakan.sandell    $t = $item['meta']->getField(array('IPTC.Headline','xmp.dc:title'));
6310b173dceSAndreas Gohr    $d = $item['meta']->getField(array('IPTC.Caption','EXIF.UserComment',
6323df72098SAndreas Gohr                                       'EXIF.TIFFImageDescription',
6333df72098SAndreas Gohr                                       'EXIF.TIFFUserComment'));
6340b173dceSAndreas Gohr    if(utf8_strlen($d) > 250) $d = utf8_substr($d,0,250).'...';
63549ac3837Shakan.sandell    $k = $item['meta']->getField(array('IPTC.Keywords','IPTC.Category','xmp.dc:subject'));
6363df72098SAndreas Gohr
6370b173dceSAndreas Gohr    // print EXIF/IPTC data
6380b173dceSAndreas Gohr    if($t || $d || $k ){
6390b173dceSAndreas Gohr        echo '<p>';
6400b173dceSAndreas Gohr        if($t) echo '<strong>'.htmlspecialchars($t).'</strong><br />';
6410b173dceSAndreas Gohr        if($d) echo htmlspecialchars($d).'<br />';
6420b173dceSAndreas Gohr        if($t) echo '<em>'.htmlspecialchars($k).'</em>';
6433df72098SAndreas Gohr        echo '</p>';
6440b173dceSAndreas Gohr    }
6453df72098SAndreas Gohr    echo '</div>';
6463df72098SAndreas Gohr}
6473df72098SAndreas Gohr
6483df72098SAndreas Gohr/**
6493df72098SAndreas Gohr * Print the media upload form if permissions are correct
6503df72098SAndreas Gohr *
6513df72098SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
6523df72098SAndreas Gohr */
6533df72098SAndreas Gohrfunction media_uploadform($ns, $auth){
6543df72098SAndreas Gohr    global $lang;
6553df72098SAndreas Gohr
6563df72098SAndreas Gohr    if($auth < AUTH_UPLOAD) return; //fixme print info on missing permissions?
6573df72098SAndreas Gohr
658d00ec455SAndreas Gohr    // The default HTML upload form
6599f5dde7fSMichael Klier    $form = new Doku_Form('dw__upload', DOKU_BASE.'lib/exe/mediamanager.php', false, 'multipart/form-data');
660d00ec455SAndreas Gohr    $form->addElement('<div class="upload">' . $lang['mediaupload'] . '</div>');
6619f5dde7fSMichael Klier    $form->addElement(formSecurityToken());
6629f5dde7fSMichael Klier    $form->addHidden('ns', hsc($ns));
6639f5dde7fSMichael Klier    $form->addElement(form_makeOpenTag('p'));
6649f5dde7fSMichael Klier    $form->addElement(form_makeFileField('upload', $lang['txt_upload'].':', 'upload__file'));
6659f5dde7fSMichael Klier    $form->addElement(form_makeCloseTag('p'));
6669f5dde7fSMichael Klier    $form->addElement(form_makeOpenTag('p'));
6679f5dde7fSMichael Klier    $form->addElement(form_makeTextField('id', '', $lang['txt_filename'].':', 'upload__name'));
6689f5dde7fSMichael Klier    $form->addElement(form_makeButton('submit', '', $lang['btn_upload']));
6699f5dde7fSMichael Klier    $form->addElement(form_makeCloseTag('p'));
670cf6894dfSAndreas Gohr
6719f5dde7fSMichael Klier    if($auth >= AUTH_DELETE){
6729f5dde7fSMichael Klier      $form->addElement(form_makeOpenTag('p'));
6739f5dde7fSMichael Klier      $form->addElement(form_makeCheckboxField('ow', 1, $lang['txt_overwrt'], 'dw__ow', 'check'));
6749f5dde7fSMichael Klier      $form->addElement(form_makeCloseTag('p'));
6753df72098SAndreas Gohr    }
6769f5dde7fSMichael Klier    html_form('upload', $form);
677d00ec455SAndreas Gohr
678d00ec455SAndreas Gohr    // prepare flashvars for multiupload
679d00ec455SAndreas Gohr    $opt = array(
680d00ec455SAndreas Gohr        'L_gridname'  => $lang['mu_gridname'] ,
681d00ec455SAndreas Gohr        'L_gridsize'  => $lang['mu_gridsize'] ,
682d00ec455SAndreas Gohr        'L_gridstat'  => $lang['mu_gridstat'] ,
683d00ec455SAndreas Gohr        'L_namespace' => $lang['mu_namespace'] ,
684d00ec455SAndreas Gohr        'L_overwrite' => $lang['txt_overwrt'],
685d00ec455SAndreas Gohr        'L_browse'    => $lang['mu_browse'],
686d00ec455SAndreas Gohr        'L_upload'    => $lang['btn_upload'],
687d00ec455SAndreas Gohr        'L_toobig'    => $lang['mu_toobig'],
688d00ec455SAndreas Gohr        'L_ready'     => $lang['mu_ready'],
689d00ec455SAndreas Gohr        'L_done'      => $lang['mu_done'],
690d00ec455SAndreas Gohr        'L_fail'      => $lang['mu_fail'],
691d00ec455SAndreas Gohr        'L_authfail'  => $lang['mu_authfail'],
692d00ec455SAndreas Gohr        'L_progress'  => $lang['mu_progress'],
693d00ec455SAndreas Gohr        'L_filetypes' => $lang['mu_filetypes'],
69458b091deSAndreas Gohr        'L_info'      => $lang['mu_info'],
69558b091deSAndreas Gohr        'L_lasterr'   => $lang['mu_lasterr'],
696d00ec455SAndreas Gohr
697d00ec455SAndreas Gohr        'O_ns'        => ":$ns",
698d00ec455SAndreas Gohr        'O_backend'   => 'mediamanager.php?'.session_name().'='.session_id(),
69958b091deSAndreas Gohr        'O_maxsize'   => php_to_byte(ini_get('upload_max_filesize')),
700d00ec455SAndreas Gohr        'O_extensions'=> join('|',array_keys(getMimeTypes())),
701d00ec455SAndreas Gohr        'O_overwrite' => ($auth >= AUTH_DELETE),
702d00ec455SAndreas Gohr        'O_sectok'    => getSecurityToken(),
703d00ec455SAndreas Gohr        'O_authtok'   => auth_createToken(),
704d00ec455SAndreas Gohr    );
7058c8dd6eaSAndreas Gohr    $var = buildURLparams($opt);
706d00ec455SAndreas Gohr    // output the flash uploader
707d00ec455SAndreas Gohr    ?>
708d00ec455SAndreas Gohr    <div id="dw__flashupload" style="display:none">
709d00ec455SAndreas Gohr    <div class="upload"><?php echo $lang['mu_intro']?></div>
710d4c61e61SAndreas Gohr    <?php echo html_flashobject('multipleUpload.swf','500','190',null,$opt); ?>
711d00ec455SAndreas Gohr    </div>
712d00ec455SAndreas Gohr    <?php
7139f5dde7fSMichael Klier}
7143df72098SAndreas Gohr
7153df72098SAndreas Gohr/**
716*bf1f3ac4Ssarnowski * Print the search field form
717*bf1f3ac4Ssarnowski *
718*bf1f3ac4Ssarnowski * @author Tobias Sarnowski <sarnowski@cosmocode.de>
719*bf1f3ac4Ssarnowski */
720*bf1f3ac4Ssarnowskifunction media_searchform($ns, $auth){
721*bf1f3ac4Ssarnowski    global $lang;
722*bf1f3ac4Ssarnowski
723*bf1f3ac4Ssarnowski    if (!isset($_REQUEST['key'])) {
724*bf1f3ac4Ssarnowski        $default = '';
725*bf1f3ac4Ssarnowski    } else {
726*bf1f3ac4Ssarnowski        $default = htmlspecialchars($_REQUEST['key']);
727*bf1f3ac4Ssarnowski    }
728*bf1f3ac4Ssarnowski
729*bf1f3ac4Ssarnowski    // The default HTML search form
730*bf1f3ac4Ssarnowski    $form = new Doku_Form('dw__mediasearch', DOKU_BASE.'lib/exe/mediamanager.php', false);
731*bf1f3ac4Ssarnowski    $form->addElement('<div class="upload">' . $lang['mediasearch'] . '</div>');
732*bf1f3ac4Ssarnowski    $form->addElement(formSecurityToken());
733*bf1f3ac4Ssarnowski    $form->addHidden('ns', hsc($ns));
734*bf1f3ac4Ssarnowski    $form->addHidden('call', 'mediasearchlist');
735*bf1f3ac4Ssarnowski    $form->addElement(form_makeOpenTag('p'));
736*bf1f3ac4Ssarnowski    $form->addElement(form_makeTextField('key', $default, $lang['txt_searchmediakey'].':', 'searchmedia__key'));
737*bf1f3ac4Ssarnowski    $form->addElement(form_makeButton('submit', '', $lang['btn_search']));
738*bf1f3ac4Ssarnowski    $form->addElement(form_makeCloseTag('p'));
739*bf1f3ac4Ssarnowski    if ($ns) {
740*bf1f3ac4Ssarnowski        $form->addElement(form_makeOpenTag('p'));
741*bf1f3ac4Ssarnowski        $form->addElement(form_makeCheckboxField('key_relative', true, sprintf($lang['txt_searchrelative'], $ns), 'dw__relative', 'check', isset($_REQUEST['key_relative'])?array('checked' => 'checked'):array()));
742*bf1f3ac4Ssarnowski        $form->addElement(form_makeCloseTag('p'));
743*bf1f3ac4Ssarnowski    }
744*bf1f3ac4Ssarnowski
745*bf1f3ac4Ssarnowski    html_form('searchmedia', $form);
746*bf1f3ac4Ssarnowski}
747*bf1f3ac4Ssarnowski
748*bf1f3ac4Ssarnowski/**
7493df72098SAndreas Gohr * Build a tree outline of available media namespaces
7503df72098SAndreas Gohr *
7513df72098SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
7523df72098SAndreas Gohr */
7533df72098SAndreas Gohrfunction media_nstree($ns){
7543df72098SAndreas Gohr    global $conf;
755256ca81eSAndreas Gohr    global $lang;
7563df72098SAndreas Gohr
7573df72098SAndreas Gohr    // currently selected namespace
7583df72098SAndreas Gohr    $ns  = cleanID($ns);
7593df72098SAndreas Gohr    if(empty($ns)){
7603df72098SAndreas Gohr        $ns = dirname(str_replace(':','/',$ID));
7613df72098SAndreas Gohr        if($ns == '.') $ns ='';
7623df72098SAndreas Gohr    }
7633df72098SAndreas Gohr    $ns  = utf8_encodeFN(str_replace(':','/',$ns));
7643df72098SAndreas Gohr
7653df72098SAndreas Gohr    $data = array();
766ee7b5a62SAndreas Gohr    search($data,$conf['mediadir'],'search_index',array('ns' => $ns, 'nofiles' => true));
7673df72098SAndreas Gohr
7683df72098SAndreas Gohr    // wrap a list with the root level around the other namespaces
769256ca81eSAndreas Gohr    $item = array( 'level' => 0, 'id' => '',
770256ca81eSAndreas Gohr                   'open' =>'true', 'label' => '['.$lang['mediaroot'].']');
7713df72098SAndreas Gohr
7723df72098SAndreas Gohr    echo '<ul class="idx">';
7733df72098SAndreas Gohr    echo media_nstree_li($item);
7743df72098SAndreas Gohr    echo media_nstree_item($item);
7753df72098SAndreas Gohr    echo html_buildlist($data,'idx','media_nstree_item','media_nstree_li');
7763df72098SAndreas Gohr    echo '</li>';
7773df72098SAndreas Gohr    echo '</ul>';
7783df72098SAndreas Gohr}
7793df72098SAndreas Gohr
7803df72098SAndreas Gohr/**
7813df72098SAndreas Gohr * Userfunction for html_buildlist
7823df72098SAndreas Gohr *
7833df72098SAndreas Gohr * Prints a media namespace tree item
7843df72098SAndreas Gohr *
7853df72098SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
7863df72098SAndreas Gohr */
7873df72098SAndreas Gohrfunction media_nstree_item($item){
7883df72098SAndreas Gohr    $pos   = strrpos($item['id'], ':');
7893df72098SAndreas Gohr    $label = substr($item['id'], $pos > 0 ? $pos + 1 : 0);
7903df72098SAndreas Gohr    if(!$item['label']) $item['label'] = $label;
7913df72098SAndreas Gohr
7923df72098SAndreas Gohr    $ret  = '';
7933df72098SAndreas Gohr    $ret .= '<a href="'.DOKU_BASE.'lib/exe/mediamanager.php?ns='.idfilter($item['id']).'" class="idx_dir">';
7943df72098SAndreas Gohr    $ret .= $item['label'];
7953df72098SAndreas Gohr    $ret .= '</a>';
7963df72098SAndreas Gohr    return $ret;
7973df72098SAndreas Gohr}
7983df72098SAndreas Gohr
7993df72098SAndreas Gohr/**
8003df72098SAndreas Gohr * Userfunction for html_buildlist
8013df72098SAndreas Gohr *
8023df72098SAndreas Gohr * Prints a media namespace tree item opener
8033df72098SAndreas Gohr *
8043df72098SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
8053df72098SAndreas Gohr */
8063df72098SAndreas Gohrfunction media_nstree_li($item){
8073df72098SAndreas Gohr    $class='media level'.$item['level'];
8083df72098SAndreas Gohr    if($item['open']){
8093df72098SAndreas Gohr        $class .= ' open';
8103df72098SAndreas Gohr        $img   = DOKU_BASE.'lib/images/minus.gif';
8117af1b404SAnika Henke        $alt   = '&minus;';
8123df72098SAndreas Gohr    }else{
8133df72098SAndreas Gohr        $class .= ' closed';
8143df72098SAndreas Gohr        $img   = DOKU_BASE.'lib/images/plus.gif';
8157af1b404SAnika Henke        $alt   = '+';
8163df72098SAndreas Gohr    }
8173df72098SAndreas Gohr    return '<li class="'.$class.'">'.
8187af1b404SAnika Henke           '<img src="'.$img.'" alt="'.$alt.'" />';
8193df72098SAndreas Gohr}
82013c08e2fSMichael Klier
82113c08e2fSMichael Klier/**
82213c08e2fSMichael Klier * Resizes the given image to the given size
82313c08e2fSMichael Klier *
82413c08e2fSMichael Klier * @author  Andreas Gohr <andi@splitbrain.org>
82513c08e2fSMichael Klier */
82613c08e2fSMichael Klierfunction media_resize_image($file, $ext, $w, $h=0){
82713c08e2fSMichael Klier  global $conf;
82813c08e2fSMichael Klier
82913c08e2fSMichael Klier  $info = @getimagesize($file); //get original size
83013c08e2fSMichael Klier  if($info == false) return $file; // that's no image - it's a spaceship!
83113c08e2fSMichael Klier
83213c08e2fSMichael Klier  if(!$h) $h = round(($w * $info[1]) / $info[0]);
83313c08e2fSMichael Klier
83413c08e2fSMichael Klier  // we wont scale up to infinity
83513c08e2fSMichael Klier  if($w > 2000 || $h > 2000) return $file;
83613c08e2fSMichael Klier
83713c08e2fSMichael Klier  //cache
83813c08e2fSMichael Klier  $local = getCacheName($file,'.media.'.$w.'x'.$h.'.'.$ext);
83913c08e2fSMichael Klier  $mtime = @filemtime($local); // 0 if not exists
84013c08e2fSMichael Klier
84113c08e2fSMichael Klier  if( $mtime > filemtime($file) ||
84213c08e2fSMichael Klier      media_resize_imageIM($ext,$file,$info[0],$info[1],$local,$w,$h) ||
84313c08e2fSMichael Klier      media_resize_imageGD($ext,$file,$info[0],$info[1],$local,$w,$h) ){
84413c08e2fSMichael Klier    if($conf['fperm']) chmod($local, $conf['fperm']);
84513c08e2fSMichael Klier    return $local;
84613c08e2fSMichael Klier  }
84713c08e2fSMichael Klier  //still here? resizing failed
84813c08e2fSMichael Klier  return $file;
84913c08e2fSMichael Klier}
85013c08e2fSMichael Klier
85113c08e2fSMichael Klier/**
85213c08e2fSMichael Klier * Crops the given image to the wanted ratio, then calls media_resize_image to scale it
85313c08e2fSMichael Klier * to the wanted size
85413c08e2fSMichael Klier *
85513c08e2fSMichael Klier * Crops are centered horizontally but prefer the upper third of an vertical
85613c08e2fSMichael Klier * image because most pics are more interesting in that area (rule of thirds)
85713c08e2fSMichael Klier *
85813c08e2fSMichael Klier * @author  Andreas Gohr <andi@splitbrain.org>
85913c08e2fSMichael Klier */
86013c08e2fSMichael Klierfunction media_crop_image($file, $ext, $w, $h=0){
86113c08e2fSMichael Klier  global $conf;
86213c08e2fSMichael Klier
86313c08e2fSMichael Klier  if(!$h) $h = $w;
86413c08e2fSMichael Klier  $info = @getimagesize($file); //get original size
86513c08e2fSMichael Klier  if($info == false) return $file; // that's no image - it's a spaceship!
86613c08e2fSMichael Klier
86713c08e2fSMichael Klier  // calculate crop size
86813c08e2fSMichael Klier  $fr = $info[0]/$info[1];
86913c08e2fSMichael Klier  $tr = $w/$h;
87013c08e2fSMichael Klier  if($tr >= 1){
87113c08e2fSMichael Klier    if($tr > $fr){
87213c08e2fSMichael Klier        $cw = $info[0];
87313c08e2fSMichael Klier        $ch = (int) $info[0]/$tr;
87413c08e2fSMichael Klier    }else{
87513c08e2fSMichael Klier        $cw = (int) $info[1]*$tr;
87613c08e2fSMichael Klier        $ch = $info[1];
87713c08e2fSMichael Klier    }
87813c08e2fSMichael Klier  }else{
87913c08e2fSMichael Klier    if($tr < $fr){
88013c08e2fSMichael Klier        $cw = (int) $info[1]*$tr;
88113c08e2fSMichael Klier        $ch = $info[1];
88213c08e2fSMichael Klier    }else{
88313c08e2fSMichael Klier        $cw = $info[0];
88413c08e2fSMichael Klier        $ch = (int) $info[0]/$tr;
88513c08e2fSMichael Klier    }
88613c08e2fSMichael Klier  }
88713c08e2fSMichael Klier  // calculate crop offset
88813c08e2fSMichael Klier  $cx = (int) ($info[0]-$cw)/2;
88913c08e2fSMichael Klier  $cy = (int) ($info[1]-$ch)/3;
89013c08e2fSMichael Klier
89113c08e2fSMichael Klier  //cache
89213c08e2fSMichael Klier  $local = getCacheName($file,'.media.'.$cw.'x'.$ch.'.crop.'.$ext);
89313c08e2fSMichael Klier  $mtime = @filemtime($local); // 0 if not exists
89413c08e2fSMichael Klier
89513c08e2fSMichael Klier  if( $mtime > filemtime($file) ||
89613c08e2fSMichael Klier      media_crop_imageIM($ext,$file,$info[0],$info[1],$local,$cw,$ch,$cx,$cy) ||
89713c08e2fSMichael Klier      media_resize_imageGD($ext,$file,$cw,$ch,$local,$cw,$ch,$cx,$cy) ){
89813c08e2fSMichael Klier    if($conf['fperm']) chmod($local, $conf['fperm']);
89913c08e2fSMichael Klier    return media_resize_image($local,$ext, $w, $h);
90013c08e2fSMichael Klier  }
90113c08e2fSMichael Klier
90213c08e2fSMichael Klier  //still here? cropping failed
90313c08e2fSMichael Klier  return media_resize_image($file,$ext, $w, $h);
90413c08e2fSMichael Klier}
90513c08e2fSMichael Klier
90613c08e2fSMichael Klier/**
90713c08e2fSMichael Klier * Download a remote file and return local filename
90813c08e2fSMichael Klier *
90913c08e2fSMichael Klier * returns false if download fails. Uses cached file if available and
91013c08e2fSMichael Klier * wanted
91113c08e2fSMichael Klier *
91213c08e2fSMichael Klier * @author  Andreas Gohr <andi@splitbrain.org>
91313c08e2fSMichael Klier * @author  Pavel Vitis <Pavel.Vitis@seznam.cz>
91413c08e2fSMichael Klier */
91513c08e2fSMichael Klierfunction media_get_from_URL($url,$ext,$cache){
91613c08e2fSMichael Klier  global $conf;
91713c08e2fSMichael Klier
91813c08e2fSMichael Klier  // if no cache or fetchsize just redirect
91913c08e2fSMichael Klier  if ($cache==0)           return false;
92013c08e2fSMichael Klier  if (!$conf['fetchsize']) return false;
92113c08e2fSMichael Klier
92213c08e2fSMichael Klier  $local = getCacheName(strtolower($url),".media.$ext");
92313c08e2fSMichael Klier  $mtime = @filemtime($local); // 0 if not exists
92413c08e2fSMichael Klier
92513c08e2fSMichael Klier  //decide if download needed:
92613c08e2fSMichael Klier  if( ($mtime == 0) ||                           // cache does not exist
92713c08e2fSMichael Klier      ($cache != -1 && $mtime < time()-$cache)   // 'recache' and cache has expired
92813c08e2fSMichael Klier    ){
92913c08e2fSMichael Klier      if(media_image_download($url,$local)){
93013c08e2fSMichael Klier        return $local;
93113c08e2fSMichael Klier      }else{
93213c08e2fSMichael Klier        return false;
93313c08e2fSMichael Klier      }
93413c08e2fSMichael Klier  }
93513c08e2fSMichael Klier
93613c08e2fSMichael Klier  //if cache exists use it else
93713c08e2fSMichael Klier  if($mtime) return $local;
93813c08e2fSMichael Klier
93913c08e2fSMichael Klier  //else return false
94013c08e2fSMichael Klier  return false;
94113c08e2fSMichael Klier}
94213c08e2fSMichael Klier
94313c08e2fSMichael Klier/**
94413c08e2fSMichael Klier * Download image files
94513c08e2fSMichael Klier *
94613c08e2fSMichael Klier * @author Andreas Gohr <andi@splitbrain.org>
94713c08e2fSMichael Klier */
94813c08e2fSMichael Klierfunction media_image_download($url,$file){
94913c08e2fSMichael Klier  global $conf;
95013c08e2fSMichael Klier  $http = new DokuHTTPClient();
95113c08e2fSMichael Klier  $http->max_bodysize = $conf['fetchsize'];
95213c08e2fSMichael Klier  $http->timeout = 25; //max. 25 sec
95313c08e2fSMichael Klier  $http->header_regexp = '!\r\nContent-Type: image/(jpe?g|gif|png)!i';
95413c08e2fSMichael Klier
95513c08e2fSMichael Klier  $data = $http->get($url);
95613c08e2fSMichael Klier  if(!$data) return false;
95713c08e2fSMichael Klier
95813c08e2fSMichael Klier  $fileexists = @file_exists($file);
95913c08e2fSMichael Klier  $fp = @fopen($file,"w");
96013c08e2fSMichael Klier  if(!$fp) return false;
96113c08e2fSMichael Klier  fwrite($fp,$data);
96213c08e2fSMichael Klier  fclose($fp);
96313c08e2fSMichael Klier  if(!$fileexists and $conf['fperm']) chmod($file, $conf['fperm']);
96413c08e2fSMichael Klier
96513c08e2fSMichael Klier  // check if it is really an image
96613c08e2fSMichael Klier  $info = @getimagesize($file);
96713c08e2fSMichael Klier  if(!$info){
96813c08e2fSMichael Klier    @unlink($file);
96913c08e2fSMichael Klier    return false;
97013c08e2fSMichael Klier  }
97113c08e2fSMichael Klier
97213c08e2fSMichael Klier  return true;
97313c08e2fSMichael Klier}
97413c08e2fSMichael Klier
97513c08e2fSMichael Klier/**
97613c08e2fSMichael Klier * resize images using external ImageMagick convert program
97713c08e2fSMichael Klier *
97813c08e2fSMichael Klier * @author Pavel Vitis <Pavel.Vitis@seznam.cz>
97913c08e2fSMichael Klier * @author Andreas Gohr <andi@splitbrain.org>
98013c08e2fSMichael Klier */
98113c08e2fSMichael Klierfunction media_resize_imageIM($ext,$from,$from_w,$from_h,$to,$to_w,$to_h){
98213c08e2fSMichael Klier  global $conf;
98313c08e2fSMichael Klier
98413c08e2fSMichael Klier  // check if convert is configured
98513c08e2fSMichael Klier  if(!$conf['im_convert']) return false;
98613c08e2fSMichael Klier
98713c08e2fSMichael Klier  // prepare command
98813c08e2fSMichael Klier  $cmd  = $conf['im_convert'];
98913c08e2fSMichael Klier  $cmd .= ' -resize '.$to_w.'x'.$to_h.'!';
99013c08e2fSMichael Klier  if ($ext == 'jpg' || $ext == 'jpeg') {
99113c08e2fSMichael Klier      $cmd .= ' -quality '.$conf['jpg_quality'];
99213c08e2fSMichael Klier  }
99313c08e2fSMichael Klier  $cmd .= " $from $to";
99413c08e2fSMichael Klier
99513c08e2fSMichael Klier  @exec($cmd,$out,$retval);
99613c08e2fSMichael Klier  if ($retval == 0) return true;
99713c08e2fSMichael Klier  return false;
99813c08e2fSMichael Klier}
99913c08e2fSMichael Klier
100013c08e2fSMichael Klier/**
100113c08e2fSMichael Klier * crop images using external ImageMagick convert program
100213c08e2fSMichael Klier *
100313c08e2fSMichael Klier * @author Andreas Gohr <andi@splitbrain.org>
100413c08e2fSMichael Klier */
100513c08e2fSMichael Klierfunction media_crop_imageIM($ext,$from,$from_w,$from_h,$to,$to_w,$to_h,$ofs_x,$ofs_y){
100613c08e2fSMichael Klier  global $conf;
100713c08e2fSMichael Klier
100813c08e2fSMichael Klier  // check if convert is configured
100913c08e2fSMichael Klier  if(!$conf['im_convert']) return false;
101013c08e2fSMichael Klier
101113c08e2fSMichael Klier  // prepare command
101213c08e2fSMichael Klier  $cmd  = $conf['im_convert'];
101313c08e2fSMichael Klier  $cmd .= ' -crop '.$to_w.'x'.$to_h.'+'.$ofs_x.'+'.$ofs_y;
101413c08e2fSMichael Klier  if ($ext == 'jpg' || $ext == 'jpeg') {
101513c08e2fSMichael Klier      $cmd .= ' -quality '.$conf['jpg_quality'];
101613c08e2fSMichael Klier  }
101713c08e2fSMichael Klier  $cmd .= " $from $to";
101813c08e2fSMichael Klier
101913c08e2fSMichael Klier  @exec($cmd,$out,$retval);
102013c08e2fSMichael Klier  if ($retval == 0) return true;
102113c08e2fSMichael Klier  return false;
102213c08e2fSMichael Klier}
102313c08e2fSMichael Klier
102413c08e2fSMichael Klier/**
102513c08e2fSMichael Klier * resize or crop images using PHP's libGD support
102613c08e2fSMichael Klier *
102713c08e2fSMichael Klier * @author Andreas Gohr <andi@splitbrain.org>
102813c08e2fSMichael Klier * @author Sebastian Wienecke <s_wienecke@web.de>
102913c08e2fSMichael Klier */
103013c08e2fSMichael Klierfunction media_resize_imageGD($ext,$from,$from_w,$from_h,$to,$to_w,$to_h,$ofs_x=0,$ofs_y=0){
103113c08e2fSMichael Klier  global $conf;
103213c08e2fSMichael Klier
103313c08e2fSMichael Klier  if($conf['gdlib'] < 1) return false; //no GDlib available or wanted
103413c08e2fSMichael Klier
103513c08e2fSMichael Klier  // check available memory
103613c08e2fSMichael Klier  if(!is_mem_available(($from_w * $from_h * 4) + ($to_w * $to_h * 4))){
103713c08e2fSMichael Klier    return false;
103813c08e2fSMichael Klier  }
103913c08e2fSMichael Klier
104013c08e2fSMichael Klier  // create an image of the given filetype
104113c08e2fSMichael Klier  if ($ext == 'jpg' || $ext == 'jpeg'){
104213c08e2fSMichael Klier    if(!function_exists("imagecreatefromjpeg")) return false;
104313c08e2fSMichael Klier    $image = @imagecreatefromjpeg($from);
104413c08e2fSMichael Klier  }elseif($ext == 'png') {
104513c08e2fSMichael Klier    if(!function_exists("imagecreatefrompng")) return false;
104613c08e2fSMichael Klier    $image = @imagecreatefrompng($from);
104713c08e2fSMichael Klier
104813c08e2fSMichael Klier  }elseif($ext == 'gif') {
104913c08e2fSMichael Klier    if(!function_exists("imagecreatefromgif")) return false;
105013c08e2fSMichael Klier    $image = @imagecreatefromgif($from);
105113c08e2fSMichael Klier  }
105213c08e2fSMichael Klier  if(!$image) return false;
105313c08e2fSMichael Klier
105413c08e2fSMichael Klier  if(($conf['gdlib']>1) && function_exists("imagecreatetruecolor") && $ext != 'gif'){
105513c08e2fSMichael Klier    $newimg = @imagecreatetruecolor ($to_w, $to_h);
105613c08e2fSMichael Klier  }
105713c08e2fSMichael Klier  if(!$newimg) $newimg = @imagecreate($to_w, $to_h);
105813c08e2fSMichael Klier  if(!$newimg){
105913c08e2fSMichael Klier    imagedestroy($image);
106013c08e2fSMichael Klier    return false;
106113c08e2fSMichael Klier  }
106213c08e2fSMichael Klier
106313c08e2fSMichael Klier  //keep png alpha channel if possible
106413c08e2fSMichael Klier  if($ext == 'png' && $conf['gdlib']>1 && function_exists('imagesavealpha')){
106513c08e2fSMichael Klier    imagealphablending($newimg, false);
106613c08e2fSMichael Klier    imagesavealpha($newimg,true);
106713c08e2fSMichael Klier  }
106813c08e2fSMichael Klier
106913c08e2fSMichael Klier  //keep gif transparent color if possible
107013c08e2fSMichael Klier  if($ext == 'gif' && function_exists('imagefill') && function_exists('imagecolorallocate')) {
107113c08e2fSMichael Klier    if(function_exists('imagecolorsforindex') && function_exists('imagecolortransparent')) {
107213c08e2fSMichael Klier      $transcolorindex = @imagecolortransparent($image);
107313c08e2fSMichael Klier      if($transcolorindex >= 0 ) { //transparent color exists
107413c08e2fSMichael Klier        $transcolor = @imagecolorsforindex($image, $transcolorindex);
107513c08e2fSMichael Klier        $transcolorindex = @imagecolorallocate($newimg, $transcolor['red'], $transcolor['green'], $transcolor['blue']);
107613c08e2fSMichael Klier        @imagefill($newimg, 0, 0, $transcolorindex);
107713c08e2fSMichael Klier        @imagecolortransparent($newimg, $transcolorindex);
107813c08e2fSMichael Klier      }else{ //filling with white
107913c08e2fSMichael Klier        $whitecolorindex = @imagecolorallocate($newimg, 255, 255, 255);
108013c08e2fSMichael Klier        @imagefill($newimg, 0, 0, $whitecolorindex);
108113c08e2fSMichael Klier      }
108213c08e2fSMichael Klier    }else{ //filling with white
108313c08e2fSMichael Klier      $whitecolorindex = @imagecolorallocate($newimg, 255, 255, 255);
108413c08e2fSMichael Klier      @imagefill($newimg, 0, 0, $whitecolorindex);
108513c08e2fSMichael Klier    }
108613c08e2fSMichael Klier  }
108713c08e2fSMichael Klier
108813c08e2fSMichael Klier  //try resampling first
108913c08e2fSMichael Klier  if(function_exists("imagecopyresampled")){
109013c08e2fSMichael Klier    if(!@imagecopyresampled($newimg, $image, 0, 0, $ofs_x, $ofs_y, $to_w, $to_h, $from_w, $from_h)) {
109113c08e2fSMichael Klier      imagecopyresized($newimg, $image, 0, 0, $ofs_x, $ofs_y, $to_w, $to_h, $from_w, $from_h);
109213c08e2fSMichael Klier    }
109313c08e2fSMichael Klier  }else{
109413c08e2fSMichael Klier    imagecopyresized($newimg, $image, 0, 0, $ofs_x, $ofs_y, $to_w, $to_h, $from_w, $from_h);
109513c08e2fSMichael Klier  }
109613c08e2fSMichael Klier
109713c08e2fSMichael Klier  $okay = false;
109813c08e2fSMichael Klier  if ($ext == 'jpg' || $ext == 'jpeg'){
109913c08e2fSMichael Klier    if(!function_exists('imagejpeg')){
110013c08e2fSMichael Klier      $okay = false;
110113c08e2fSMichael Klier    }else{
110213c08e2fSMichael Klier      $okay = imagejpeg($newimg, $to, $conf['jpg_quality']);
110313c08e2fSMichael Klier    }
110413c08e2fSMichael Klier  }elseif($ext == 'png') {
110513c08e2fSMichael Klier    if(!function_exists('imagepng')){
110613c08e2fSMichael Klier      $okay = false;
110713c08e2fSMichael Klier    }else{
110813c08e2fSMichael Klier      $okay =  imagepng($newimg, $to);
110913c08e2fSMichael Klier    }
111013c08e2fSMichael Klier  }elseif($ext == 'gif') {
111113c08e2fSMichael Klier    if(!function_exists('imagegif')){
111213c08e2fSMichael Klier      $okay = false;
111313c08e2fSMichael Klier    }else{
111413c08e2fSMichael Klier      $okay = imagegif($newimg, $to);
111513c08e2fSMichael Klier    }
111613c08e2fSMichael Klier  }
111713c08e2fSMichael Klier
111813c08e2fSMichael Klier  // destroy GD image ressources
111913c08e2fSMichael Klier  if($image) imagedestroy($image);
112013c08e2fSMichael Klier  if($newimg) imagedestroy($newimg);
112113c08e2fSMichael Klier
112213c08e2fSMichael Klier  return $okay;
112313c08e2fSMichael Klier}
112413c08e2fSMichael Klier
1125365be586SAndreas Gohr/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
1126