Changeset 1159 for trunk/common


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/common
Files:
4 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'; 
Note: See TracChangeset for help on using the changeset viewer.