diff --git a/.gitignore b/.gitignore index 9eab7de..ed39763 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -local/epfl/cache/*.txt +local/epfl/deprecated/cache/*.txt diff --git a/install/install_bins.sh b/install/install_bins.sh index e7c4690..f59e388 100755 --- a/install/install_bins.sh +++ b/install/install_bins.sh @@ -1,84 +1,84 @@ #!/bin/bash # Set SNAME, SDIR and SPATH if [ -h $0 ]; then SNAME=$(readlink $0); else SNAME=$0; fi SDIR=$(dirname $SNAME) if [[ "$SDIR" =~ ^/ ]]; then SPATH=$SDIR; else SPATH=$(realpath $(pwd)/$SDIR); fi # Set DIR_LIB DIR_LIB=$(realpath $SPATH/../lib/bash) # DEBUG STUFF #echo "SNAME: $SNAME" #echo "SDIR: $SDIR" #echo "SPATH: $SPATH" #echo "DIR_LIB: $DIR_LIB" # Source libs source $DIR_LIB/tools.sh source $DIR_LIB/colors.sh source $DIR_LIB/io.sh # Set DESTINATION if [ -n "$1" ]; then DESTINATION="$(realpath $1)" else DESTINATION=$(realpath ${HOME}/bin) fi if [ ! -d $DESTINATION/ ]; then error_echo "'$DESTINATION' is not a valid directory." exit 1 fi info_echo "Using '$DESTINATION' as binary directory." info_echo "To change binary directory, run \"$(basename $0) /path/to/your/bin_dir\"" confirm "Do you want to continue using \"$DESTINATION\" as binary directory?" +if [ $? -eq 0 ]; then exit; fi; function link_to_binaries() { for bin in $(echo $BINS); do if [ -r $DESTINATION/$bin ]; then if [ -h $DESTINATION/$bin ]; then OLD=$(realpath $DESTINATION/$bin) if [ "$OLD" == "$DIR_BIN/$bin" ]; then color_echo_n "Command '$bin' already exists... " else color_echo_n "Moving command '$bin' from '$OLD' to '$DIR_BIN/$bin'... " rm $DESTINATION/$bin ln -s $DIR_BIN/$bin $DESTINATION/$bin fi else error_echo "A similar command already exists: '$OLD'" exit 2 fi else color_echo_n "Installing command '$bin' to '$DESTINATION/'... " ln -s $DIR_BIN/$bin $DESTINATION/$bin fi [ -h $DESTINATION/$bin ] check_rc_echo $? if [ $(command $bin 2> /dev/null | wc -l) -eq 0 ]; then warning_echo "Make sure '$DESTINATION' is in your \$PATH variable. (Looks like it is not the case.)"; fi done - } DIR_BIN=$(realpath $SPATH/../bin) BINS='bamc' link_to_binaries DIR_BIN=$(realpath $SPATH/../local/epfl) -BINS='search-epfl check-scipers' +BINS='sel check-scipers' link_to_binaries if [ "$OS" == "darwin" ]; then if [ "$CLICOLOR" != "1" ]; then info_echo "To have output colors in your terminal, set CLICOLOR=1 in your environment." fi fi # RIP exit 0 diff --git a/local/epfl/check-scipers b/local/epfl/check-scipers index ffc81fe..c1b65c0 100755 --- a/local/epfl/check-scipers +++ b/local/epfl/check-scipers @@ -1,134 +1,162 @@ -#!/bin/bash - -# Set SNAME, SDIR and SPATH -if [ -h $0 ]; then SNAME=$(readlink $0); else SNAME=$0; fi -SDIR=$(dirname $SNAME) -if [[ "$SDIR" =~ ^/ ]]; then SPATH=$SDIR; else SPATH=$(realpath $(pwd)/$SDIR); fi - -#================================= -# Set DIR_LIB and source libs -DIR_LIB=$(realpath $SPATH/../../lib/bash) -source $DIR_LIB/tools.sh -source $DIR_LIB/io.sh -source $DIR_LIB/cmdline_parser.sh -source $DIR_LIB/debugging.sh - -# Set DIR_CACHE -DIR_CACHE=$(realpath $SPATH/cache/) - -DEFAULT_ACTIONS="help" -DEFAULT_PARAMS="name=1,sciper=2" -DEFAULT_ITEMS="$(find . -type f -name '*\.csv' | sort)" - -function usage() { - echo "USAGE: $(basename $SNAME) --only scipers_1.csv[,scipers_2.csv] --params name=1,sciper=2,[refetch] [help|fetch|csv]"; +#!/usr/bin/env php + /dev/null - if [ $? -eq 1 ]; then - fetch=1 - else - if [ $(cat $cache_file | wc -l) -eq 0 ]; then - fetch=1 - fi - fi - fi - - - if [ $fetch -eq 1 ]; then - search-epfl -s $sciper > $cache_file - DATA=$(search-epfl -s $sciper | tr '\n' '~'); - else - DATA=$(cat $cache_file | tr '\n' '~') - fi +# check file exists +if (!is_file($options['ifile'])) { + echo "File not found: ".$options['ifile']."\n"; + exit; } -function get_field() { - local field=$1 - echo $DATA | tr '~' '\n' | grep "^$field:" | cut -d ':' -f 2- -} +# read file +$idata = file($options['ifile'], FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES); + +# remove header if present +if ($idata[0] == $options['iformat']) array_shift($idata); + +# print output header +echo $options['oformat']."\n"; + +$LDAP = new EPFL_LDAP($options['hostname'], $options['port']); + +# process lines +foreach ($idata as $i => $iline) { + $iline = explode($options['iseparator'], $iline); + // Format input_student + $input_student = array(); + foreach (array_keys($oformat) as $key) { + switch ($key) { + case 'id': + $input_student[$key] = $i; + break; + default: + if (array_key_exists($key, $iformat)) { + $input_student[$key] = $iline[$iformat[$key]]; + } else { + $input_student[$key] = null; + } + } + } + $input_student['email'] = 'n/a'; + + # fetch LDAP data + $LDAP->find_by_sciper($input_student['sciper'], 'student', 1); + $output_student = $LDAP->get_entries(); + if (count($output_student) == 1) { + $output_student = $output_student[0]; + } else { + $output_student = array( 'found' => 0 ); + } + + # add some info + if ((bool)$output_student['found']) { + $output_student['status'] = 'OK'; + } else { + $output_student['status'] = 'NOT FOUND'; + } + + # Merge info + $merged_student = array_merge($input_student, $output_student); + +# print_r($input_student); +# print_r($output_student); +# print_r($merged_student); + + # Print line + $oline = array(); + foreach (array_keys($oformat) as $key) $oline[] = $merged_student[$key]; + echo implode($options['oseparator'], $oline)."\n"; -run $@ +} -# RIP -exit 0 +#print_r($idata); + +exit; + + +$LDAP = new EPFL_LDAP($hostname, $port); + + + +if (!empty($options['s']) or !empty($options['sciper'])) { + $sciper = empty($options['s']) ? $options['sciper'] : $options['s']; + $type = null; + if (!empty($options['t']) or !empty($options['type'])) $type = empty($options['t']) ? $options['type'] : $options['t']; + try { + $LDAP->find_by_sciper($sciper, $type, $limit); + } catch (Exception $e) { + } +} else { + $entries = null; + if (!empty($options['g']) or !empty($options['givenname'])) { + $name = empty($options['g']) ? $options['givenname'] : $options['n']; + try { + $LDAP->find_by_givenname($name, $limit); + } catch (Exception $e) { + $entries = null; + } + } elseif (!empty($options['n']) or !empty($options['name'])) { + $name = empty($options['n']) ? $options['name'] : $options['n']; + try { + $LDAP->find_by_surname($name, $limit); + } catch (Exception $e) { + $entries = null; + } + } +} +$entries = $LDAP->get_entries(); +// Display +if (!is_null($entries)) { + foreach ($entries as $entry) $Exporter->to_bash($entry); +} else { + $Exporter->to_bash(array('found' => 0)); +} +?> diff --git a/local/epfl/cache/readme b/local/epfl/deprecated/cache/readme similarity index 100% rename from local/epfl/cache/readme rename to local/epfl/deprecated/cache/readme diff --git a/local/epfl/lib/people_search.php b/local/epfl/deprecated/people_search.php similarity index 100% rename from local/epfl/lib/people_search.php rename to local/epfl/deprecated/people_search.php diff --git a/local/epfl/search-epfl b/local/epfl/deprecated/search-epfl similarity index 100% rename from local/epfl/search-epfl rename to local/epfl/deprecated/search-epfl diff --git a/local/epfl/lib/lib_sel.php b/local/epfl/lib/lib_sel.php new file mode 100755 index 0000000..38f7256 --- /dev/null +++ b/local/epfl/lib/lib_sel.php @@ -0,0 +1,194 @@ +ldap_port = $ldap_port; + $this->ldap_host = $ldap_host; + $this->ldap_dn = "o=epfl,c=ch"; + $this->ldap_connector = ldap_connect($ldap_host, $ldap_port) or die("Could not connect to $ldap_host:$ldap_port."); + $this->ldap_attributes = array('personaltitle', 'uniqueidentifier', 'mail', 'sn', 'givenname', 'edupersonaffiliation', 'ou', 'title'); + } + + protected function find_by_filter($filter, $limit) { + $ldap_results = null; + $this->ldap_entries = null; + $this->formatted_entries = null; + $ldap_results = @ldap_search($this->ldap_connector, $this->ldap_dn, $filter, $this->ldap_attributes, 0, $limit); + #$ldap_results = @ldap_search($this->ldap_connector, $this->ldap_dn, $filter); + $this->ldap_entries = ldap_get_entries($this->ldap_connector, $ldap_results); + $count = $this->ldap_entries['count']; + if ($count) { + $this->format_entries(); + return $count; + } + return false; + } + + public function find_by_sciper($sciper, $type = null, $limit = 50) { + if (is_null($sciper) or !is_numeric($sciper) or ($sciper < 100000) or ($sciper > 999999)) + throw new Exception("Invalid sciper: '$sciper'"); + $filter = 'uniqueIdentifier='.$sciper; + if (!is_null($type)) { + $filter = "(&($filter)(eduPersonAffiliation=$type))"; + } + return $this->find_by_filter($filter, $limit); + } + + public function find_by_givenname($name, $limit = 50) { + $filter = 'givenname='.$name; + return $this->find_by_filter($filter, $limit); + } + + public function find_by_surname($name, $limit = 50) { + $filter = 'sn='.$name; + return $this->find_by_filter($filter, $limit); + } + + public function find_by_ldap_filter($filter, $limit = 50) { + return $this->find_by_filter($filter, $limit); + } + + protected function format_entries() { + $this->formatted_entries = array(); + + foreach ($this->ldap_entries as $entry) { + if (! is_array($entry)) continue; + $formatted_entry = array( 'found' => 1 ); + + # Sex + switch ($entry['personaltitle'][0]) { + case 'Madame': + $formatted_entry['sex'] = 'F'; + break; + case 'Monsieur': + $formatted_entry['sex'] = 'M'; + break; + default: + $formatted_entry['sex'] = 'X'; + } + + # SCIPER + $formatted_entry['sciper'] = $entry['uniqueidentifier'][0]; + + # email address + if (!array_key_exists('mail', $entry)) { + $formatted_entry['email'] = $formatted_entry['sciper'].'@epfl.ch'; + } else { + $formatted_entry['email'] = $entry['mail'][0]; + + } + + # Surname + $i = null; + if (array_key_exists(2, $entry['sn'])) { + $i = 2; + } elseif (array_key_exists(0, $entry['sn'])) { + $i = 0; + } + if (is_null($i)) { + $formatted_entry['surname'] = 'X'; + } else { + $formatted_entry['surname'] = $entry['sn'][$i]; + } + + # Givenname + $i = null; + if (array_key_exists(2, $entry['givenname'])) { + $i = 2; + } elseif (array_key_exists(0, $entry['givenname'])) { + $i = 0; + } + if (is_null($i)) { + $givenname = 'X'; + } else { + $givenname = $entry['givenname'][$i]; + } + $email_givenname_size = strlen(explode('.', $formatted_entry['email'])[0]); + $givennames = explode(' ', $givenname); + $i = 1; + $final_givenname = $givennames[0]; + $max = count($givennames)-1; + while ((mb_strlen($final_givenname, 'UTF-8') < $email_givenname_size) and ($i <= $max)) { + $final_givenname .= ' '.$givennames[$i]; + $i++; + } + + # if email as a '-', put it back in the given name is missing + if (preg_match('/-/', explode('.', $formatted_entry['email'])[0]) and preg_match('/ /', $final_givenname)) { + $final_givenname = preg_replace('/ /', '-', $final_givenname); + } + $formatted_entry['givenname'] = $final_givenname; + + $formatted_entry['name'] = $formatted_entry['surname']." ".$formatted_entry['givenname']; + + # Type + $formatted_entry['type'] = $entry['edupersonaffiliation'][0]; + + # Details + switch ($formatted_entry['type']) { + case 'student': + if (array_key_exists(0, $entry['ou'])) { + if (preg_match('/-/', $entry['ou'][0])) { + $formatted_entry['section'] = explode('-', $entry['ou'][0])[0]; + $formatted_entry['cursus'] = explode('-', $entry['ou'][0])[1]; + } else { + $formatted_entry['section'] = $entry['ou'][0]; + $formatted_entry['cursus'] = 'XX'; + } + } else { + $formatted_entry['section'] = 'XXX'; + $formatted_entry['cursus'] = 'XX'; + } + break; + case 'staff': + if (array_key_exists('ou', $entry)) { + if (array_key_exists(0, $entry['ou'])) $formatted_entry['where'] = $entry['ou'][0]; + if (array_key_exists(1, $entry['ou'])) $formatted_entry['where_1'] = $entry['ou'][1]; + } + if (array_key_exists('title', $entry)) { + if (array_key_exists(0, $entry['title'])) $formatted_entry['title'] = $entry['title'][0]; + if (array_key_exists(1, $entry['title'])) $formatted_entry['title_1'] = $entry['title'][1]; + } + break; + } + + # Add the newly formatted entry + $this->formatted_entries[] = $formatted_entry; + } + + } + + public function get_entry() { + if (!is_null($this->formatted_entries) and array_key_exists(0, $this->formatted_entries)) + return $this->formatted_entries[0]; + else + return null; + } + + public function get_entries() { + return $this->formatted_entries; + } +} + + +class Exporter { + public function to_bash($array) { + if (is_array($array)) + foreach($array as $key => $value) echo "$key:$value\n"; + } +} + + + +?> diff --git a/local/epfl/sel b/local/epfl/sel new file mode 100755 index 0000000..c203267 --- /dev/null +++ b/local/epfl/sel @@ -0,0 +1,87 @@ +#!/usr/bin/env php +find_by_sciper($sciper, $type, $limit); + } catch (Exception $e) { + } +} else { + $entries = null; + if (!empty($options['g']) or !empty($options['givenname'])) { + $name = empty($options['g']) ? $options['givenname'] : $options['n']; + try { + $LDAP->find_by_givenname($name, $limit); + } catch (Exception $e) { + $entries = null; + } + } elseif (!empty($options['n']) or !empty($options['name'])) { + $name = empty($options['n']) ? $options['name'] : $options['n']; + try { + $LDAP->find_by_surname($name, $limit); + } catch (Exception $e) { + $entries = null; + } + } +} + +$entries = $LDAP->get_entries(); +// Display +if (!is_null($entries)) { + foreach ($entries as $entry) $Exporter->to_bash($entry); +} else { + $Exporter->to_bash(array('found' => 0)); +} + +?>