Changeset 1159 for trunk


Ignore:
Timestamp:
12/07/11 17:51:00 (6 months ago)
Author:
adrian.budau
Message:

Fix more errors that occured durring the deployment of the avatar optimisation

Fixes to the attachments control. Users can't upload an attachment called avatar.
Now the avatar can be deleted.
Fixes to the image resize script regarding gif files (it used to enter an infinite loop)

REVIEW URL http://reviewboard.infoarena.ro/r/181/

Location:
trunk
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/common/attachment.php

    r1155 r1159  
    178178            $trans_col = imagecolortransparent($image); 
    179179            imagepalettecopy($image_resized, $image); 
    180             imagefill($image_resized, 0, 0, $trans_col); 
     180            if ($trans_col != -1) { 
     181                imagefill($image_resized, 0, 0, $trans_col); 
     182            } 
    181183            imagecolortransparent($image_resized, $trans_col); 
    182184            imagecopyresampled($image_resized, $image, 0, 0, 0, 0, 
  • trunk/common/avatar.php

    r1156 r1159  
    77require_once(IA_ROOT_DIR.'www/controllers/account_validator.php'); 
    88require_once(IA_ROOT_DIR.'common/common.php'); 
     9require_once(IA_ROOT_DIR.'www/config.php'); 
    910require_once(IA_ROOT_DIR.'common/attachment.php'); 
     11 
     12/** 
     13 * Returns whether the attachment of the given page is an avatar attachment 
     14 * @param  string  $attachment_name 
     15 * @param  string  $page_name 
     16 * @return bool 
     17 */ 
     18function is_avatar_attachment($attachment_name, $page_name) { 
     19    $matches = get_page_user_name($page_name); 
     20 
     21    if ($attachment_name === 'avatar' && $matches) { 
     22        return true; 
     23    } 
     24 
     25    return false; 
     26} 
     27 
     28/** 
     29 * Resizes a newly uploaded avatar and returns errors if any 
     30 * @param  string  $temporary_name 
     31 * @param  string  $filepath          The filepath where to copy the attachment 
     32 * @param  string  $username 
     33 * @return mixed   Error message or null on success 
     34 */ 
     35function avatar_update($temporary_name, $filepath, $username) { 
     36    // resize the avatar if it has a correct mime-type 
     37    $avatar_mime_types = array('image/gif', 'image/jpeg', 'image/png'); 
     38    $image_info = getimagesize($temporary_name); 
     39    if (!in_array($image_info['mime'], $avatar_mime_types)) { 
     40        return 'Fisierul nu este o imagine acceptata pe site. ' . 
     41                'Utilizati doar imagini GIF, JPEG sau PNG.'; 
     42    } 
     43 
     44    // write the file on disk. 
     45    if (!move_uploaded_file($temporary_name, $filepath)) { 
     46        return 'Fisierul nu a putut fi incarcat pe server.'; 
     47    } 
     48    // resize the avatar 
     49    avatar_cache_resized($filepath, $image_info, "a".$username); 
     50    return null; 
     51} 
    1052 
    1153/** 
     
    4587} 
    4688 
     89/** 
     90 * Delete's an user avatar, the rest is done from the attachment page 
     91 * 
     92 * @param  string  $username 
     93 */ 
     94function avatar_delete($username) { 
     95    $resize_folders = array('tiny/', 'small/', 'normal/', 'forum/', 'big/'); 
     96 
     97    // Unlink the hardlinked full-sized image 
     98    $filepath = IA_AVATAR_FOLDER . 'full/a' . $username; 
     99    if (is_file($filepath) || is_link($filepath)) { 
     100        unlink($filepath); 
     101    } 
     102 
     103    // Delete the resized ones 
     104    foreach ($resize_folders as $resize_folder) { 
     105        $filepath = IA_AVATAR_FOLDER . $resize_folder . 'a' 
     106                . $username; 
     107        if (is_file($filepath) || is_link($filepath)) { 
     108            unlink($filepath); 
     109        } 
     110    } 
     111} 
    47112?> 
  • trunk/common/common.php

    r1155 r1159  
    154154function is_page_name($page_name) { 
    155155    return preg_match('/^'.IA_RE_PAGE_NAME.'$/xi', $page_name); 
     156} 
     157 
     158/** 
     159 * Validates user page name 
     160 * 
     161 * @param  string  $page_name 
     162 * @return array                returns an array containing the matched user 
     163 */ 
     164function get_page_user_name($page_name) { 
     165    preg_match("/^ ". 
     166                preg_quote(IA_USER_TEXTBLOCK_PREFIX, '/'). 
     167                '('.IA_RE_USER_NAME.") (\/?.*) $/xi", 
     168                $page_name, $matches); 
     169    return $matches; 
    156170} 
    157171 
  • trunk/common/security.php

    r1150 r1159  
    193193    // HACK: Forward security to user. 
    194194    // HACK: based on name 
    195     if (preg_match("/^ ". 
    196                 preg_quote(IA_USER_TEXTBLOCK_PREFIX, '/'). 
    197                 '('.IA_RE_USER_NAME.") (\/?.*) $/xi", 
    198                 $textblock['name'], $matches)) { 
     195    if (count($matches = get_page_user_name($textblock['name'])) > 0) { 
    199196        require_once(IA_ROOT_DIR . "common/db/user.php"); 
    200197        $ouser = user_get_by_username($matches[1]); 
     
    205202        // This is a horrible hack to prevent deleting or moving an user page. 
    206203        // This is pure evil. 
    207         if ($matches[2] != '') { 
    208             return false; 
    209         } 
    210204        if ($action == 'textblock-delete' || $action == 'textblock-move') { 
    211205            $action = 'simple-critical'; 
  • trunk/scripts/setup

    r1155 r1159  
    230230    system("chmod g+ws {$ia_root}attach"); 
    231231    system("chmod g+ws {$ia_root}cache"); 
    232     system("chmod g+ws {$ia_root}www/static/images/avatar"); 
    233232    system("chmod g+ws {$ia_root}www/static/images/tmp"); 
    234233    system("chmod g+ws {$ia_root}www/static/images/latex"); 
     
    269268} 
    270269 
     270// Running scripts to keep things simple for new developers 
     271system("./{$ia_root}scripts/make-avatar-folder"); 
    271272// FIXME: configure forum 
    272273if (read_bool("Should I try to configure the forum (ugly db stuff)?", true)) { 
  • trunk/www/controllers/account.php

    r1155 r1159  
    77require_once(IA_ROOT_DIR."www/controllers/account_validator.php"); 
    88require_once(IA_ROOT_DIR."www/config.php"); 
     9require_once(IA_ROOT_DIR."common/avatar.php"); 
    910 
    1011// identify target user and check permission to edit profile 
     
    129130                if (!$errors) { 
    130131                    $disk_name = attachment_get_filepath($attach); 
    131                     if (!move_uploaded_file($_FILES['avatar']['tmp_name'], 
    132                                             $disk_name)) { 
    133                         $errors['avatar'] = 'Fisierul nu a putut fi incarcat ' 
    134                                             .'pe server.'; 
    135                     } else { 
    136                         // resize the avatar if it is a correct mime-type 
    137                         global $IA_SAFE_MIME_TYPES; 
    138                         $img_info = getimagesize($disk_name); 
    139                         // check if mime-type is from accepted ones 
    140                         if (in_array($img_info['mime'], $IA_SAFE_MIME_TYPES)) { 
    141                             avatar_cache_resized($disk_name, $img_info, 
    142                                     "a".$user['username']); 
    143                         } 
    144                     } 
     132                    $errors['avatar'] = avatar_update( 
     133                            $_FILES['avatar']['tmp_name'], $disk_name, 
     134                            $user['username']); 
    145135                } 
    146136            } 
     
    212202    $view['form_values'] = $data; 
    213203    $view['action'] = url_account($user['username']); 
     204    $view['avatar_exists'] = attachment_get('avatar', IA_USER_TEXTBLOCK_PREFIX . 
     205            $user['username']); 
    214206    if ($ownprofile) { 
    215207        $view['topnav_select'] = 'profile'; 
  • trunk/www/controllers/attachment.php

    r1115 r1159  
    66require_once(IA_ROOT_DIR.'www/controllers/zip_attachment.php'); 
    77require_once(IA_ROOT_DIR."common/external_libs/zipfile.php"); 
     8require_once(IA_ROOT_DIR."common/avatar.php"); 
    89 
    910// Try to get the textblock model for a certain page. 
     
    109110    if (!$form_errors) { 
    110111        if ($autoextract) { 
    111             $zip_files = get_zipped_attachments($_FILES['file_name']['tmp_name']); 
     112            $zip_files = get_zipped_attachments( 
     113                    $_FILES['file_name']['tmp_name']); 
    112114 
    113115            if (false === $zip_files) { 
    114                 $form_errors['file_name'] = 'Arhiva ZIP este invalida sau nu poate fi recunoscuta'; 
     116                $form_errors['file_name'] = 'Arhiva ZIP este invalida sau nu ' 
     117                        . 'poate fi recunoscuta'; 
    115118            } else { 
    116119                $attachments = $zip_files['attachments']; 
    117                 $skipped_files = $zip_files['total_files'] - count($attachments); 
     120                $skipped_files = $zip_files['total_files'] - 
     121                        count($attachments); 
    118122            } 
    119123        } 
     
    121125            // simple (single) file attachment 
    122126            $attachments = array( 
    123                 array('name' => $form_values['file_name'], 'size' => $form_values['file_size'], 
    124                       'disk_name' => $_FILES['file_name']['tmp_name']) 
    125             ); 
     127                array('name' => $form_values['file_name'], 
     128                        'size' => $form_values['file_size'], 
     129                        'disk_name' => $_FILES['file_name']['tmp_name'])); 
    126130        } 
    127131    } 
     
    141145            $tmpname = tempnam(IA_ROOT_DIR . 'attach/', 'iatmp'); 
    142146            log_assert($tmpname); 
    143             $res = extract_zipped_attachment($ziparchive, $att['zipindex'], $tmpname); 
     147            $res = extract_zipped_attachment($ziparchive, $att['zipindex'], 
     148                    $tmpname); 
    144149            if ($res) { 
    145150                $att['disk_name'] = $tmpname; 
     
    165170    $rewrite_count = 0; 
    166171    $attach_okcount = 0; 
     172    $extra_errors = ''; 
    167173    if (!$form_errors) { 
    168174        for ($i = 0; $i < count($attachments); $i++) { 
     
    180186                } 
    181187                $file_att['size'] = filesize($file_att['disk_name']); 
     188            } 
     189 
     190            if (is_avatar_attachment($file_att['name'], $page_name)) { 
     191                if (isset($skipped_files)) { 
     192                    $skipped_files++; 
     193                } 
     194                $extra_errors .= ' A fost intalnit un fisier cu numele avatar' . 
     195                        '. Pentru a va modifica imaginea de profil va rugam ' . 
     196                        'folositi pagina "Contul meu".'; 
     197                 continue; 
    182198            } 
    183199 
     
    216232            $disk_name = attachment_get_filepath($file_att['attach_obj']); 
    217233            if (is_uploaded_file($file_att['disk_name'])) { 
    218                 $move_ok = move_uploaded_file($file_att['disk_name'], $disk_name); 
     234                $move_ok = move_uploaded_file($file_att['disk_name'], 
     235                        $disk_name); 
    219236            } else { 
    220237                $move_ok = @rename($file_att['disk_name'], $disk_name); 
     
    232249 
    233250    // custom error message for simple (single) file uploads 
    234     if (!$form_errors && !$autoextract && 0 >= $attach_okcount) { 
    235         $form_errors['file_name'] = 'Fisierul nu a putut fi atasat! Eroare necunoscuta ...'; 
     251    if (!$form_errors && !$autoextract && 0 >= $attach_okcount && 
     252                !$extra_errors) { 
     253        $form_errors['file_name'] = 'Fisierul nu a putut fi atasat! Eroare ' . 
     254                'necunoscuta ...'; 
    236255    } 
    237256 
     
    240259        if ($autoextract) { 
    241260            if ($attach_okcount == 1) { 
    242                 $msg = "Am extras si incarcat un fisier."; 
     261                $msg = 'Am extras si incarcat un fisier.'; 
    243262            } else { 
    244263                $msg = "Am extras si incarcat {$attach_okcount} fisiere."; 
     
    246265 
    247266            if ($rewrite_count == 1) { 
    248                 $msg .= " Un fisier mai vechi a fost rescris."; 
     267                $msg .= ' Un fisier mai vechi a fost rescris.'; 
    249268            } else if ($rewrite_count > 1) { 
    250269                $msg .= " {$rewrite_count} fisiere mai vechi au fost rescrise."; 
     
    252271 
    253272            if ($skipped_files == 1) { 
    254                 $msg .= " Un fisier nu a fost dezarhivat deoarece era prea mare sau era invalid."; 
     273                $msg .= ' Un fisier nu a fost dezarhivat deoarece era prea ' . 
     274                        'mare sau era invalid.'; 
    255275            } else if ($skipped_files > 1) { 
    256                 $msg .= " {$skipped_files} fisiere nu au fost dezarhivate deoarece erau prea mari sau erau invalide."; 
    257             } 
     276                $msg .= " {$skipped_files} fisiere nu au fost dezarhivate " . 
     277                        'deoarece erau prea mari sau erau invalide.'; 
     278            } 
     279            $msg .= $extra_errors; 
    258280        } 
    259281        else { 
    260             if ($rewrite_count) { 
    261                 $msg = "Fisierul trimis a fost atasat cu succes. Un atasamant mai vechi a fost rescris."; 
    262             } 
    263             else { 
    264                 $msg = "Fisierul trimis a fost atasat cu succes."; 
     282            if ($extra_errors) { 
     283                $msg = $extra_errors; 
     284            } else if($rewrite_count) { 
     285                $msg = 'Fisierul trimis a fost atasat cu succes. Un atasament' . 
     286                        'mai vechi a fost rescris.'; 
     287            } else { 
     288                $msg = 'Fisierul trimis a fost atasat cu succes.'; 
    265289            } 
    266290        } 
     
    270294    } 
    271295 
     296    $form_errors['file_name'] .= $extra_errors; 
    272297    // Errors, print view template. 
    273298    $view['form_errors'] = $form_errors; 
     
    301326    } 
    302327 
     328    // Delete the resizedimages in case the page is an avatar 
     329    $matches = get_page_user_name($page_name); 
     330    if (is_avatar_attachment($file_name, $page_name)) { 
     331        avatar_delete($matches[1]); 
     332    } 
    303333    // We've got big balls. 
    304334 
     
    342372    if ($old_name == $new_name) { 
    343373        redirect(url_attachment_list($page_name)); 
     374    } 
     375 
     376    if (is_avatar_attachment($old_name, $page_name)) { 
     377        flash_error('Atasamentul "avatar" nu poate fi redenumit.'); 
     378        redirect(url_textblock($page_name)); 
     379    } 
     380 
     381    if (is_avatar_attachment($new_name, $page_name)) { 
     382        flash_error('Nu puteti numi un atasament "avatar". Pentru ' 
     383                . 'a va modifica imaginea de profil va rugam folositi ' 
     384                . 'pagina "Contul meu".'); 
     385        redirect(url_textblock($page_name)); 
    344386    } 
    345387 
  • trunk/www/index.php

    r1157 r1159  
    280280    } 
    281281} 
    282 else if ($action == 'download-zip') { 
     282else if ($action == 'attach-bulk-action') { 
    283283    require_once(IA_ROOT_DIR.'www/controllers/attachment.php'); 
    284284    if(request('download')) { 
  • trunk/www/views/account.php

    r1155 r1159  
    9797        </li> 
    9898        <li> 
     99            <?php 
     100                if ($view['avatar_exists']) { 
     101                    echo format_post_link(url_attachment_delete( 
     102                                IA_USER_TEXTBLOCK_PREFIX . $user['username'], 
     103                                'avatar'), "Sterge Avatar", array(), true, 
     104                                array('onclick' => "return confirm('Aceasta " . 
     105                                        'actiune este ireversibila! Doresti ' . 
     106                                        "sa continui?')")); 
     107                } 
     108            ?> 
    99109            <label for="form_avatar">Avatar nou</label> 
    100110            <?= ferr_span('avatar') ?> 
  • trunk/www/views/listattach.php

    r1073 r1159  
    3737function format_attach_name($row) { 
    3838    global $page_name; 
    39      
     39 
    4040    $attachurl = '<span id="rename_'.$row['id'].'" style="display: none">'; 
    4141    $attachurl .= '<form action="'.url_attachment_rename($page_name).'" method="post">'; 
     
    126126); 
    127127?> 
    128     <form method = 'post' action = ''>  
    129     <input type = 'hidden' name = 'action' value = 'download-zip'> 
     128    <form method = 'post' action = ''> 
     129    <input type = 'hidden' name = 'action' value = 'attach-bulk-action'> 
    130130    <h1>Atasamente pentru pagina <?= format_link(url_textblock($view['page_name']), $view['page_name']) ?></h1> 
    131131    <?php 
Note: See TracChangeset for help on using the changeset viewer.