1 #!/bin/bash
  2 # Script: update-script
  3 # Description: Updates http://crew.c-base.org/ - can be called by cron as user www-data
  4 # Author: Brian Wiborg <baccenfutter@c-base.org>
  5 # Version : 1.0
  6 # Date: 2011-05-08
  7 # Source: git://dev.c-base.org/crew-c-base-org/crew-c-base-org.git
  8 # License: Creative Commons Attribution 3.0 Unsupported (CC BY 3.0)
  9 
 10 grouping_limit=$((26 * 3))
 11 
 12 ##### FILES #####
 13 index_file=/var/www/crew.c-base.org/index.html
 14 member_file=/var/www/crew.c-base.org/member/memberlist.txt
 15 staging_file=/var/www/crew.c-base.org/testindex.html
 16 html_header=/var/www/crew.c-base.org/template-crew_header.html
 17 html_footer=/var/www/crew.c-base.org/template-crew_footer.html
 18 dontlist_filename=".dontlist"
 19 
 20 ##### GLOBAL ARRAYS #####
 21 declare -a all_homes
 22 declare -a ldap_users
 23 
 24 ##### PLACEHOLDER VARS #####
 25 clearboth="</div><div style=\"clear:both;\"></div>"
 26 
 27 ##### FUNCTIONS #####
 28 ### LDAP ###
 29 get_all_active_members () {
 30     local users=$(ldapsearch -x -b 'ou=crew,dc=c-base,dc=org' '(&(objectClass=cbaseMember)(memberOf=cn=crew,ou=groups,dc=c-base,dc=org))' uid | grep "uid:"| cut -d " " -f 2)
 31     ldap_users=($(echo ${users[@]} | tr ' ' '\n' | sort -n))
 32 }
 33 is_user_active () {
 34     local member="$1"
 35     for ldap_user in ${ldap_users[@]}; do
 36         if [[ "$ldap_user" == "$member" ]]; then
 37             return 0
 38         fi
 39     done
 40     return 1
 41 }
 42 ### HOMES ###
 43 get_all_home_dirs () {
 44     for dir in /home/*/; do
 45         local member=$(basename ${dir} /)
 46         if is_index_file "$member"; then
 47             www_homes+=("${dir}")
 48         fi
 49     done
 50 }
 51 ### FILTERS ###
 52 index_file_is_default () {
 53     local member="$1"
 54     local index_html="$2"
 55     local default_string="<html><head><title>crewmember: ${member} </title></head><body bgcolor='black'></body></html>"
 56     grep -q "$default_string" "$index_html"
 57 }
 58 index_file_has_more_lines_than () {
 59     local index_html="$1"
 60     local min_line_count="$2"
 61     (( $(wc -l < "$index_html") > "$min_line_count" ))
 62 }
 63 dontlist_file_exists () {
 64     local member="$1"
 65     local dontlist="/home/${member}/public_html/${dontlist_filename}"
 66     [[ -f "$dontlist" ]]
 67 }
 68 is_index_file () {
 69     local member="$1"
 70     local member_html_dir="/home/$member/public_html/"
 71     local index_html="$member_html_dir/index.html"
 72     local index_php="$member_html_dir/index.php"
 73     # disqualify un-active and filter un-willing members
 74     is_user_active "$member" || return 1
 75     dontlist_file_exists "$member" && return 1
 76     [[ -r "$index_php" ]] && index_file_has_more_lines_than "$index_php" 1 && return 0
 77     [[ -r "$index_html" ]] && ! index_file_is_default "$member" "$index_html" && index_file_has_more_lines_than "$index_html" 3 && return 0
 78 }
 79 # HTML #
 80 write_html_header () {
 81     cat "$html_header"
 82 }
 83 write_html_footer () {
 84     cat "$html_footer"
 85 }
 86 render_memberbox () {
 87     local member="$1"
 88     local gravatar_url="$2"
 89     cat <<EOF
 90     <div class="memberbox">
 91         <div class="member-icon">
 92             <img src="${gravatar_url}" alt="${gravatar_url}" />
 93         </div>
 94         [ <span class="crewname"><a href="https://${member}.crew.c-base.org/">${member}</a></span> ]<br />
 95     </div>
 96 EOF
 97 }
 98 render_first_letter_and_hr () {
 99     local member=$1
100     cat <<EOF
101         ${clearboth}
102         <br /><hr />
103         <br /><h2>${member:0:1}</h2><br />
104         <div class="memberbox-wrapper">
105 EOF
106 }
107 group_by_first_letter () {
108     # this function does <div>-voodoo-magic to group all members
109     # by the first letter of their crewname
110     local member="$1"
111     local first_letter="$2"
112     if [[ ! ${member:0:1} == "$first_letter" ]]; then
113         render_first_letter_and_hr "$member" >> "$staging_file"
114         first_letter=${member:0:1}
115     fi
116     echo "$first_letter"
117 }
118 render_memberbox_wrapper () {
119     local first_letter="" # needed for group_by_first_letter()
120     echo "<div class=\"memberbox-wrapper\">"
121     for dir in ${www_homes[@]}; do
122         local member="$(basename ${dir} /)"
123         local md5hash=$(echo -n "${member}@c-base.org" | md5sum)
124         local gravatar_url="http://gravatar.com/avatar/$md5hash"
125         # the following condition takes care of grouping members by first letter once the grouping limit is reached
126         (( ${#www_homes[@]} > $grouping_limit )) && first_letter="$(group_by_first_letter ${member} ${first_letter})"
127         render_memberbox "$member" "$gravatar_url"
128     done
129     cat <<EOF
130     </div><br />
131     ${clearboth}
132     <br /><hr />
133     Last update @ CET:$(date +%Y-%m-%d--%H:%M) found ${#www_homes[@]} member-pages from a total of ${#ldap_users[@]} crew-members.
134     <hr /><br /><br />
135 EOF
136 }
137 update_index_file () {
138     cp "$staging_file" "$index_file"
139     chown -R www-data:www-data "$staging_file" "$index_file"
140     chmod 755 "$staging_file" "$index_file"
141 }
142 
143 user_finger_toget_sirname () {
144     local member="$1"
145     sirname=$(finger "$member" | head -n1 | tr ':' '\n' | tail -n1 | tr ' ' '\n' | tail -n 1)
146     totalsize=${#sirname}
147     securename=${sirname:${#sirname}-3:${#sirname}}
148     asteriskcount=$(( totalsize - 3 ))
149     for asterisk in $(seq 1 ${asteriskcount}); do
150         securename="*$securename"
151     done
152     echo $securename
153 
154 }
155 
156 ### MAIN ###
157 get_all_active_members
158 get_all_home_dirs
159 write_html_header > "$staging_file"
160 render_memberbox_wrapper >> "$staging_file"
161 write_html_footer >> "$staging_file"
162 update_index_file
163 
164 #echo "<b>Achtung:</b> Einige Nachnamen werden inkorrekt dargestellt, was auf Zeichensatzprobleme im OpenLDAP zurueckzufuehren ist!" > "$member_file"
165 #echo "membername   nachname" >> "$member_file"
166 #echo "======================" >> "$member_file"
167 #for user in ${ldap_users[@]}; do
168 #   echo "$user <-> $(user_finger_toget_sirname ${user})" >> "$member_file"
169 #done
170 ## fix character encoding
171 #mv $member_file $member_file.tmp
172 #iconv -ct utf-8 $member_file.tmp > $member_file
173 #rm $member_file.tmp