4images Modifications / Modifikationen > Mods & Plugins (Releases & Support)

[Mod] Avatar v2.01

(1/101) > >>

V@no:
Avatar v2

Some people were interesting in such a mod.
This mod will let users select an avatar for their profiles.
The avatars will be displayed in comments. ( can be added somewhere else, if needed )

New features in v2:
1. users can upload their own avatars.
2. administrator can anable/disable: using avatars, upload avatars, change alowed width/height.


This MOD was writen and tested on Windows system with 4images v1.7
---------------------------------------------------------------------------------------------------------------
Changes in following files:


/member.php
/details.php
/includes/upload.php
/includes/db_field_definitions.php
/admin/settings.php
/admin/admin_functions.php
/lang/<yourlanguage>/admin.php
/lang/<yourlanguage>/main.php
/templates/<yourtemplate>/member_profile.html
/templates/<yourtemplate>/member_editprofile.html
/templates/<yourtemplate>/comment_bit.html


---------------------------------------------------------------------------------------

Step 1.
Open member.php

Find:
--- Code: ---      "user_email_save" => $user_email_save,
--- End code ---

Insert below:
--- Code: ---     "lang_avatar" => $lang['avatar'],
     "user_avatar_current" => ($config['avatar_use']) ? "<img src=\"".TEMPLATE_PATH."/avatars/".(($user_row['user_avatar'] == "") ? "blank.gif" : $user_row['user_avatar'])."\" name=\"icons\" border=\"0\" alt=\"\">" : "",

--- End code ---


1.2.
Find:
--- Code: ---   if (!$error) {
   $additional_sql = "";
    if (!empty($additional_user_fields)) {
      $table_fields = $site_db->get_table_fields(USERS_TABLE);
      foreach ($additional_user_fields as $key => $val) {
        if (isset($HTTP_POST_VARS[$key]) && isset($table_fields[$key])) {
          $additional_sql .= ", $key = '".un_htmlspecialchars(trim($HTTP_POST_VARS[$key]))."'";
        }
      }
    }

    $sql = "UPDATE ".USERS_TABLE."
            SET ".get_user_table_field("", "user_email")." = '$user_email', ".get_user_table_field("", "user_showemail")." = $user_showemail, ".get_user_table_field("", "user_allowemails")." = $user_allowemails, ".get_user_table_field("", "user_invisible")." = $user_invisible, ".get_user_table_field("", "user_homepage")." = '$user_homepage', ".get_user_table_field("", "user_icq")." = '$user_icq'".$additional_sql."
            WHERE ".get_user_table_field("", "user_id")." = ".$user_info['user_id'];
    $site_db->query($sql);
    $msg_color = 1;
    $msg .= $lang['update_profile_success'];
    if (!empty($new_email_msg)) {
      $msg .= "<br />".$new_email_msg;
    }
    $user_info = $site_sess->load_user_info($user_info['user_id']);
  }
  else {
    $update_process = 1;
  }
  $action = "editprofile";
}
--- End code ---

Add above it:

--- Code: ---// Upload Avatar file
  if (!$error) {
     if (!empty($HTTP_POST_FILES['avatar_file']['tmp_name']) && $HTTP_POST_FILES['avatar_file']['tmp_name'] != "none") {
    include(ROOT_PATH.'includes/upload.php');
    $site_upload = new Upload();
    $new_name = $site_upload->upload_file("avatar_file", "avatar", $config['template_dir'], $user_info['user_id']);
     if (!$new_name) {
     $msg .= (($msg != "") ? "<br />" : "")."<b>".$lang['file_upload_error'].": ".$new_name."</b><br />".$site_upload->get_upload_errors();
     $error = 1;
     }else{
    $HTTP_POST_VARS['user_avatar'] = "users/".$new_name;
    $avatars_dir = TEMPLATE_DIR."/".$config['template_dir']."/avatars/users/";
    $dir = opendir($avatars_dir);
    $contents = array();
    while ($contents[] = readdir($dir)){;}
    closedir($dir);
    foreach ($contents as $line){
      $filename = substr($line,0,(strlen($line)-strlen(strrchr($line,"."))));
      $extension = substr(strrchr($line,"."), 1);
      $filename2 = substr($new_name,0,(strlen($new_name)-strlen(strrchr($new_name,"."))));
      $extension2 = substr(strrchr($new_name,"."), 1);
      if ($filename == $filename2 && $extension != $extension2) {
      unlink($avatars_dir.$line);
      }
    }

     }
    }
  }
// End Avatar file

--- End code ---


1.3.
Find:
--- Code: ---    $user_icq = $user_info['user_icq'];

--- End code ---

Add after:
--- Code: ---  $user_avatar = $user_info['user_avatar'];

--- End code ---


1.4.
Few lines below find:
--- Code: ---  $site_template->register_vars(array(
    "user_name" => htmlspecialchars(stripslashes($user_name)),

--- End code ---

Replace with:
--- Code: ---//-----------------------
//------ Avatar ---------
//-----------------------
  if ($config['avatar_use']){
   $images = "";
  $checked = ($user_avatar == "blank.gif" || $user_avatar == "") ? " selected" : "";
  $images .= "\n<option value=\"blank.gif\"$checked>none</option>\n";
  $dir = opendir(TEMPLATE_PATH."/avatars/users/");
  $contents = array();
  while ($contents[] = readdir($dir)){;}
  closedir($dir);
  natcasesort ($contents);
  foreach ($contents as $line){
   $filename = substr($line,0,(strlen($line)-strlen(strrchr($line,"."))));
   if ($filename == $user_info['user_id']) {
     $checked = (stristr($user_avatar, "users/")) ? " selected" : "";
     $images .= "\n<option value=\"users/$line\"$checked>".$lang['custom']."</option>\n";
   }
  }
   $dir = opendir(TEMPLATE_PATH."/avatars/");
   $contents = array();
   while ($contents[] = readdir($dir)){;}
   closedir($dir);
   natcasesort ($contents);
   $checked = "";
   foreach ($contents as $line){
      $filename = substr($line,0,(strlen($line)-strlen(strrchr($line,"."))));
      $extension = substr(strrchr($line,"."), 1);
      $checked = "";
      if ($line == $user_avatar) { $checked = " selected"; }
      if (strcasecmp($extension,"gif")==0 || strcasecmp($extension,"jpg")==0 || strcasecmp($extension,"jpeg")==0 || strcasecmp($extension,"png")==0 ){
         if ($line != "blank.gif") {
        $filename = str_replace("_", " ", $filename);
        $images .= "<option value=\"$line\"$checked>$filename</option>\n";
       }
      }
   }
  }
//----------------------
//----- End Avatar -----
//----------------------

  $site_template->register_vars(array(
   "lang_avatar" => $lang['avatar'],
   "lang_avatar_file" => $lang['avatar_file'],
   "lang_avatar_dim" => $lang['avatar_max_dim']." ".$config['avatar_width']."x".$config['avatar_height'].$lang['px'],
   "lang_avatar_select" => $lang['avatar_select'],
   "user_avatar_images" => $images,
   "user_avatar_current" => ($config['avatar_use']) ? "<img src=\"".TEMPLATE_PATH."/avatars/".(($user_avatar == "") ? "blank.gif" : $user_avatar)."\" name=\"icons\" border=\"0\" alt=\"\">" : "",
   "lang_or" => $lang['or'],
   "user_avatar_file" => $config['avatar_user_custom'],
    "user_name" => htmlspecialchars(stripslashes($user_name)),
--- End code ---



Step 2.
Open details.php
Find:
--- Code: ---      $comment_user_id = $comment_row[$i]['user_id'];

--- End code ---

Add after:
--- Code: ---    $user_row_comment = get_user_info($comment_user_id);

--- End code ---


2.2.
Find:
--- Code: ---        "comment_id" => $comment_row[$i]['comment_id'],

--- End code ---

Add after:
--- Code: ---    "user_avatar_current" => ($config['avatar_use']) ? "<img src=\"".TEMPLATE_PATH."/avatars/".(($user_row_comment['user_avatar'] == "") ? "blank.gif" : $user_row_comment['user_avatar'])."\" name=\"icons\" border=\"0\" hspace=\"15\" alt=\"\">" : "",

--- End code ---




Step 3.
Open /includes/upload.php
Find:
--- Code: ---    $this->max_height['media'] = $config['max_image_height'];

    $this->max_size['thumb'] = $config['max_thumb_size'] * 1024;
    $this->max_size['media'] = $config['max_media_size'] * 1024;

--- End code ---

Replace with:
--- Code: ---    $this->max_height['media'] = $config['max_image_height'];
  $this->max_width['avatar'] = $config['avatar_width'];
  $this->max_height['avatar'] = $config['avatar_height'];

    $this->max_size['thumb'] = $config['max_thumb_size'] * 1024;
    $this->max_size['media'] = $config['max_media_size'] * 1024;
    $this->max_size['avatar'] = 99999999999;

--- End code ---



3.2.
Find:
--- Code: ---  function copy_file() {

--- End code ---

Add after:
--- Code: ---  if ($this->image_type == "avatar") {
    if (file_exists($this->upload_path[$this->image_type]."/".$this->file_name)) {
        @unlink($this->upload_path[$this->image_type]."/".$this->file_name);
      }
      $ok = move_uploaded_file($this->upload_file, $this->upload_path[$this->image_type]."/".$this->file_name);
  }else{

--- End code ---

3.3.
At the end of this function find:
--- Code: ---    @chmod($this->upload_path[$this->image_type]."/".$this->file_name, CHMOD_FILES);

--- End code ---
Replace with:
--- Code: ---  }
    @chmod($this->upload_path[$this->image_type]."/".$this->file_name, CHMOD_FILES);

--- End code ---


3.4.
Find:
--- Code: ---    if ($cat_id) {
      $this->upload_path['thumb'] = THUMB_PATH."/".$cat_id;
      $this->upload_path['media'] = MEDIA_PATH."/".$cat_id;
    }
    else {
      $this->upload_path['thumb'] = THUMB_TEMP_PATH;
      $this->upload_path['media'] = MEDIA_TEMP_PATH;
    }

    if ($file_name != "") {

--- End code ---

Replace with:
--- Code: ---    if ($cat_id) {
      $this->upload_path['thumb'] = THUMB_PATH."/".$cat_id;
      $this->upload_path['media'] = MEDIA_PATH."/".$cat_id;
    $this->upload_path['avatar'] = TEMPLATE_DIR."/".$cat_id."/avatars/users";
    }
    else {
      $this->upload_path['thumb'] = THUMB_TEMP_PATH;
      $this->upload_path['media'] = MEDIA_TEMP_PATH;
    }

    if ($file_name != "" && $this->image_type != "avatar") {

--- End code ---


3.5.
Find:
--- Code: ---    $this->mime_type = $this->HTTP_POST_FILES[$this->field_name]['type'];

--- End code ---

Replace with:
--- Code: --- if ($this->image_type == "avatar") {
    $this->file_name = $file_name.".".$this->extension;
  }

    $this->mime_type = $this->HTTP_POST_FILES[$this->field_name]['type'];

--- End code ---


3.6.
Find:
--- Code: ---    //Thumbnails
    $this->accepted_mime_types['thumb'] = array(
--- End code ---

Insert above:
--- Code: ---  //Avatar
    $this->accepted_mime_types['avatar'] = array(
      "image/jpeg",
      "image/pjpeg",
      "image/gif",
      "image/x-png"
    );
    $this->accepted_extensions['avatar'] = array(
      "jpg",
      "jpeg",
      "gif",
      "png"
    );

--- End code ---


3.7. *new
Find:
--- Code: ---      $error_msg .= "<b>".$this->file_name.":</b> ".$msg."<br />";

--- End code ---

Replace with:
--- Code: ---      $error_msg .= "<b>".(($this->image_type == "avatar") ? $this->HTTP_POST_FILES[$this->field_name]['name'] : $this->file_name).":</b> ".$msg."<br />";

--- End code ---




Step 4.
Open /includes/db_field_definitions.php file.
Add this line:
--- Code: ---$additional_user_fields['user_avatar'] = array($lang['avatar'], "avatar", 0);

--- End code ---




Step 5.
Open /admin/settings.php
Find:
--- Code: ---  show_setting_row("highlight_admin", "radio");

--- End code ---

Add after:
--- Code: ---  show_table_separator($setting_group[8], 2, "#setting_group_8");
  show_setting_row("avatar_use", "radio");
  show_setting_row("avatar_user_custom", "radio");
  show_setting_row("avatar_width");
  show_setting_row("avatar_height");

--- End code ---
If u installed some other MOD for example "Annotation by SLL" then u probably will need change $setting_group[8], 2, "#setting_group_8");




Step 6. *updated
Open /admin/admin_functions.php
Find:
--- Code: ---      case "text":
      default:
        show_input_row($val[0], $field_name, $value);
    
      } // end switch
    }
  }
}
?>
--- End code ---

Replace with:
--- Code: ---   case "avatar":
    show_avatar_row($val[0], $field_name, $value);
    break;
      case "text":
      default:
        show_input_row($val[0], $field_name, $value);
    
      } // end switch
    }
  }
}
//-----------------------
//------ Avatar ---------
//-----------------------
function show_avatar_row($title, $name, $value = "blank.gif"){
  global $config;
  if ($config['avatar_use']){
  $dir = opendir(TEMPLATE_PATH."/avatars/");
  $contents = array();
  while ($contents[] = readdir($dir)){;}
  closedir($dir);
  natcasesort ($contents);
  echo "<tr width=\"50%\"class=\"".get_row_bg()."\" valign='top'>\n<td><p class=\"rowtitle\">".$title."</p></td>\n";
  echo "<td width=\"50%\" height=\"115\" valign=\"middle\">\n<table>\n<tr>\n<td>\n<SELECT name=\"$name\" size=\"6\" onkeypress=\"if(window.event.keyCode==13){this.form.submit();}\" onChange=\"document.form.icons_$name.src='".TEMPLATE_PATH."/avatars/'+document.form.$name.options[document.form.$name.selectedIndex].value;\">";
   if ($value == "blank.gif" || $value == "") {
   $checked = " selected";
   }else{
   $checked = "";
   }
   echo "<option value=\"blank.gif\"$checked>none</option>\n";
  foreach ($contents as $line){
     $filename = substr($line,0,(strlen($line)-strlen(strrchr($line,"."))));
     $extension = substr(strrchr($line,"."), 1);
     $checked = "";
     if ($line == $value) { $checked = " selected"; }
     if (strcasecmp($extension,"gif")==0 || strcasecmp($extension,"jpg")==0 || strcasecmp($extension,"jpeg")==0 || strcasecmp($extension,"png")==0 ){
      if ($line != "blank.gif") {
      $filename = str_replace("_", " ", $filename);
      echo "<option value=\"$line\"$checked>$filename</option>\n";
      }
     }
  }
  echo "</select>\n</td>\n<td valign='middle' align='left'>\n<img align='center' src=\"".TEMPLATE_PATH."/avatars/".(($value == "") ? "blank.gif" : $value)."\" name=\"icons_$name\" border=\"0\" alt=\"\">\n</td>\n</tr>\n</table>\n</td>\n";
  }
}
//----- End Avatar -----
?>
--- End code ---




Step 7.
Open /lang/<yourlanguage>/admin.php
Add at the end of the file, just before <? :
--- Code: ---/*-- Setting-Group 8 --*/
$setting_group[8]="Avatar";
$setting['avatar_use'] = "Use avatars";
$setting['avatar_user_custom'] = "Allow users use their own avatars";
$setting['avatar_width'] = "Max. avatar width in pixel";
$setting['avatar_height'] = "Max. avatar height in pixel";

--- End code ---
Change $setting_group[8]="Avatar"; to the number u set in /admin/settings.php




Step 8.
Open /lang/<yourlanguage>/main.php
Add at the end of the file, just before <? :
--- Code: ---$lang['avatar'] = "Avatar: ";
$lang['avatar_file'] = "Upload avatar:";
$lang['avatar_max_dim'] = "Max. allowed";
$lang['avatar_select'] = "Select from the list:";
$lang['custom'] = "Custom";

--- End code ---




Step 9.
Open /templates/<yourtemplate>/member_profile.html
Add this:
--- Code: ---        {if user_avatar_current}
        <TR>
          <TD class="row2"><B>{lang_avatar}</B></TD>
          <TD class="row2">{user_avatar_current}</TD>
        </TR>
        {endif user_avatar_current}

--- End code ---




Step 10.
Open /templates/<yourtemplate>/member_editprofile.html
Change top form to this:
--- Code: ---<FORM method="post" action="{url_member}" name="creator" enctype="multipart/form-data">
  <TABLE width="100%" border="0" cellspacing="0" cellpadding="1">
    <TR>
      <TD valign="top" class="head1">
        <TABLE width="100%" border="0" cellpadding="4" cellspacing="0">
          <TR>
            <TD colspan="2" valign="top" class="head1">{lang_profile_of} {user_name}</TD>
          </TR>
          <TR>
            <TD class="row1"><B>{lang_email}</B></TD>
            <TD class="row1"><INPUT type="text" name="user_email"  size="30" value="{user_email}" class="input" /></TD>
          </TR>
          <TR>
            <TD class="row2"><B>{lang_email_confirm}</B></TD>
            <TD class="row2"><INPUT type="text" name="user_email2"  size="30" value="{user_email2}" class="input" /></TD>
          </TR>
          <TR>
            <TD class="row1"><B>{lang_show_email}</B></TD>
            <TD class="row1">
              <INPUT type="radio" name="user_showemail" value="1"{user_showemail_yes} />
              {lang_yes}&nbsp;&nbsp;&nbsp;
              <INPUT type="radio" name="user_showemail" value="0"{user_showemail_no} />
              {lang_no}</TD>
          </TR>
      <TR>
            <TD class="row2"><B>{lang_allow_emails}</B></TD>
            <TD class="row2">
              <INPUT type="radio" name="user_allowemails" value="1"{user_allowemails_yes} />
              {lang_yes}&nbsp;&nbsp;&nbsp;
              <INPUT type="radio" name="user_allowemails" value="0"{user_allowemails_no} />
              {lang_no}
      </TD>
          </TR>
          <TR>
            <TD class="row1"><B>{lang_invisible}</B></TD>
            <TD class="row1">
              <INPUT type="radio" name="user_invisible" value="1"{user_invisible_yes} />
              {lang_yes}&nbsp;&nbsp;&nbsp;
              <INPUT type="radio" name="user_invisible" value="0"{user_invisible_no} />
              {lang_no}
      </TD>
          </TR>
          <TR>
            <TD colspan="2" class="head1">{lang_optional_infos}</TD>
          </TR>
          <TR>
            <TD class="row1"><B>{lang_homepage}</B></TD>
            <TD class="row1"><INPUT type="text" name="user_homepage"  size="30" value="{user_homepage}" class="input" /></TD>
          </TR>
          <TR>
            <TD class="row2"><B>{lang_icq}</B></TD>
            <TD class="row2"><INPUT type="text" name="user_icq"  size="30" value="{user_icq}" class="input" /></TD>
          </TR>
                   {if user_avatar_images}
          <TR>
            <TD colspan="2" class="head1">{lang_avatar}</TD>
          </TR>

          <TR>
            <TD class="row2" valign="top">
            {if user_avatar_file}
              <B>{lang_avatar_file}</B><BR />
              <SPAN class="smalltext">
              <B>{lang_avatar_dim}</B>
              </SPAN>
              <BR>
              <b>{lang_or}</b><BR>
              {endif user_avatar_file}
              <B>
              {lang_avatar_select}
              </B>
            </TD>
            <TD class="row2">
            {if user_avatar_file}
              {lang_upload}
              <INPUT type="file" name="avatar_file"  size="30" class="input" />
              <BR />
              <BR />
              {endif user_avatar_file}
              <SELECT name="user_avatar" size="6" onkeypress="if(window.event.keyCode==13){ this.form.submit(); }" onChange="document.images.icons.src='{template_url}/avatars/'+this.value;">{user_avatar_images}</SELECT>
              <TABLE width="100%" height="100" border="0">
                <TR>
                  <TD align="center">
                    {user_avatar_current}
                  </TD>
                </TR>
              </TABLE>
            </TD>
          </TR>
          {endif user_avatar_images}
        </TABLE>
      </TD>
    </TR>
  </TABLE>
  <INPUT type="hidden" name="action" value="updateprofile" />
  <P align="center">
    <INPUT type="submit" value="{lang_save}" class="button" />
    <INPUT type="reset" value="{lang_reset}" class="button" />
  </P>
</FORM>
--- End code ---



Step 11.
Open /templates/<yourtemplate>/comment_bit.html
Add this:
--- Code: --- {if user_avatar_current}
  {user_avatar_current}
  {endif user_avatar_current}

--- End code ---




Step 12.
Download attached package
Upload  install_avatar.php into your 4images root folder, and blank.gif into new /templates/<yourtemplate>/avatars
Start the installation by http://yourhost.com/your4images/install_avatar.php


Now, create two folders:
/templates/<yourtemplate>/avatars/users/
Upload your avatars in /avatars/

Now users can sellect their avatar from control panel.

P.S. Thank SLL for the installer :)
P.P.S. This MOD probably wont work if u have integrated 4images with some boards.

V@no:
The marked ( * ) code above has been changed.

Jasondavis:
Hey this is very cool....it would be cool if the user can upload there pic for the avator...think this is possible?

V@no:
I dont think my PHP knowlege let me do that...
maybe somebody esle can do? ;)

Jasondavis:
You did a great job, its just that would be the reason for me having avators is to let them have there own pic u know.  Like I think it would be possible, especially since you made it that far....just has to be a way to let them upload there pic to a folder then call it to be there avator somehow...lol here I am with my dreams i dont know ANY PHP, you did an awsome job!

Navigation

[0] Message Index

[#] Next page

Go to full version