<commit_before><?php

/**
 *  ocs-webserver
 *
 *  Copyright 2016 by pling GmbH.
 *
 *    This file is part of ocs-webserver.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU Affero General Public License as
 *    published by the Free Software Foundation, either version 3 of the
 *    License, or (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Created: 01.08.2018
 */
class Default_Model_Ocs_Ident
{
    /** @var string */
    protected $baseDn;
    /** @var Zend_Config */
    protected $config;
    protected $errMessages;
    protected $errCode;
    /** @var Zend_Ldap */
    private $identServer;

    /**
     * @inheritDoc
     */
    public function __construct($config = null)
    {
        if (isset($config)) {
            $this->config = $config;
        } else {
            $this->config = Zend_Registry::get('config')->settings->server->ldap;
        }
        $this->baseDn = $this->config->baseDn;
        $this->errMessages = array();
        $this->errCode = 0;
    }

    /**
     * @param int $member_id
     *
     * @return bool
     * @throws Zend_Exception
     * @throws Zend_Ldap_Exception
     */
    public function updateMail($member_id)
    {
        $connection = $this->getServerConnection();
        $member_data = $this->getMemberData($member_id);

        try {
            $entry = $this->getEntry($member_data, $connection);
        } catch (Exception $e) {
            $this->errMessages[] = "Failed.";
            Zend_Registry::get('logger')->err(__METHOD__ . ' - ' . $e->getMessage());

            return false;
        }
        if (empty($entry)) {
            $this->errMessages[] = "Failed.";
            Zend_Registry::get('logger')->err(__METHOD__ . ' - ldap entry for member does not exists. Going to create it.');

            return false;
        }

        $oldUidAttribute = Zend_Ldap_Attribute::getAttribute($entry, 'email');
        Zend_Ldap_Attribute::removeFromAttribute($entry, 'uid', $oldUidAttribute);
        Zend_Ldap_Attribute::removeFromAttribute($entry, 'email', $oldUidAttribute);
        Zend_Ldap_Attribute::setAttribute($entry, 'email', $member_data['email_address']);
        Zend_Ldap_Attribute::setAttribute($entry, 'uid', $member_data['email_address'], true);
        $dn = $entry['dn'];
        $connection->update($dn, $entry);
        $connection->getLastError($this->errCode, $this->errMessages);

        return true;
    }
    
    
    /**
     * @param int $member_id
     *
     * @return bool
     * @throws Zend_Exception
     * @throws Zend_Ldap_Exception
     */
    public function updateAvatar($member_id)
    {
        $connection = $this->getServerConnection();
        $member_data = $this->getMemberData($member_id);

        try {
            $entry = $this->getEntry($member_data, $connection);
        } catch (Exception $e) {
            $this->errMessages[] = "Failed.";
            Zend_Registry::get('logger')->err(__METHOD__ . ' - ' . $e->getMessage());

            return false;
        }
        if (empty($entry)) {
            $this->errMessages[] = "Failed.";
            Zend_Registry::get('logger')->err(__METHOD__ . ' - ldap entry for member does not exists. Going to create it.');

            return false;
        }

        Zend_Ldap_Attribute::removeFromAttribute($entry, 'userPassword', Zend_Ldap_Attribute::getAttribute($entry, 'jpegPhoto'));
        //$avatar = file_get_contents($member_data['profile_image_url']);
        //$avatarBase64 = base64_decode($avatar);
        //Zend_Ldap_Attribute::setAttribute($entry, 'jpegPhoto', $avatarBase64);
        $avatar = $member_data['profile_image_url'];
        $avatarJpeg = imagejpeg(imagecreatefromstring(file_get_contents($avatar)));
        $avatarBase64 = base64_decode($avatarJpeg);
        
        Zend_Ldap_Attribute::setAttribute($entry, 'jpegPhoto', $avatarBase64);
        
        $dn = $entry['dn'];
        $connection->update($dn, $entry);
        $connection->getLastError($this->errCode, $this->errMessages);

        return true;
    }

    /**<commit_msg>avatar<commit_after><?php

/**
 *  ocs-webserver
 *
 *  Copyright 2016 by pling GmbH.
 *
 *    This file is part of ocs-webserver.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU Affero General Public License as
 *    published by the Free Software Foundation, either version 3 of the
 *    License, or (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Created: 01.08.2018
 */
class Default_Model_Ocs_Ident
{
    /** @var string */
    protected $baseDn;
    /** @var Zend_Config */
    protected $config;
    protected $errMessages;
    protected $errCode;
    /** @var Zend_Ldap */
    private $identServer;

    /**
     * @inheritDoc
     */
    public function __construct($config = null)
    {
        if (isset($config)) {
            $this->config = $config;
        } else {
            $this->config = Zend_Registry::get('config')->settings->server->ldap;
        }
        $this->baseDn = $this->config->baseDn;
        $this->errMessages = array();
        $this->errCode = 0;
    }

    /**
     * @param int $member_id
     *
     * @return bool
     * @throws Zend_Exception
     * @throws Zend_Ldap_Exception
     */
    public function updateMail($member_id)
    {
        $connection = $this->getServerConnection();
        $member_data = $this->getMemberData($member_id);

        try {
            $entry = $this->getEntry($member_data, $connection);
        } catch (Exception $e) {
            $this->errMessages[] = "Failed.";
            Zend_Registry::get('logger')->err(__METHOD__ . ' - ' . $e->getMessage());

            return false;
        }
        if (empty($entry)) {
            $this->errMessages[] = "Failed.";
            Zend_Registry::get('logger')->err(__METHOD__ . ' - ldap entry for member does not exists. Going to create it.');

            return false;
        }

        $oldUidAttribute = Zend_Ldap_Attribute::getAttribute($entry, 'email');
        Zend_Ldap_Attribute::removeFromAttribute($entry, 'uid', $oldUidAttribute);
        Zend_Ldap_Attribute::removeFromAttribute($entry, 'email', $oldUidAttribute);
        Zend_Ldap_Attribute::setAttribute($entry, 'email', $member_data['email_address']);
        Zend_Ldap_Attribute::setAttribute($entry, 'uid', $member_data['email_address'], true);
        $dn = $entry['dn'];
        $connection->update($dn, $entry);
        $connection->getLastError($this->errCode, $this->errMessages);

        return true;
    }
    
    
    /**
     * @param int $member_id
     *
     * @return bool
     * @throws Zend_Exception
     * @throws Zend_Ldap_Exception
     */
    public function updateAvatar($member_id)
    {
        $connection = $this->getServerConnection();
        $member_data = $this->getMemberData($member_id);

        try {
            $entry = $this->getEntry($member_data, $connection);
        } catch (Exception $e) {
            $this->errMessages[] = "Failed.";
            Zend_Registry::get('logger')->err(__METHOD__ . ' - ' . $e->getMessage());

            return false;
        }
        if (empty($entry)) {
            $this->errMessages[] = "Failed.";
            Zend_Registry::get('logger')->err(__METHOD__ . ' - ldap entry for member does not exists. Going to create it.');

            return false;
        }

        Zend_Ldap_Attribute::removeFromAttribute($entry, 'userPassword', Zend_Ldap_Attribute::getAttribute($entry, 'jpegPhoto'));
        //$avatar = file_get_contents($member_data['profile_image_url']);
        //$avatarBase64 = base64_decode($avatar);
        //Zend_Ldap_Attribute::setAttribute($entry, 'jpegPhoto', $avatarBase64);
        $avatar = $member_data['profile_image_url'];
        $avatarJpeg = imagejpeg(imagecreatefromstring(file_get_contents($avatar)), 'avatra.jpeg');
        $avatarBase64 = base64_decode($avatarJpeg);
        
        Zend_Ldap_Attribute::setAttribute($entry, 'jpegPhoto', $avatarBase64);
        
        $dn = $entry['dn'];
        $connection->update($dn, $entry);
        $connection->getLastError($this->errCode, $this->errMessages);

        return true;
    }

    /**
{'fraction_non_alphanumeric': 0.13611294948157954, 'fraction_numerical': 0.005735715861460402, 'mean_word_length': 2.5598743619945035, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 73, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1078203', 'n_tokens_mistral': 2685, 'n_tokens_neox': 2635, 'n_words': 599}