<commit_before><?php
/**
 * rtkUser adds utility methods to the sfUser class.
 * 
 * Methods
 *   getUserId()
 *   getUserName()
 *   getUserTimeZone()
 *   getUserSequence()
 *
 *   getUserSetting()                Proxy and cache for UsersSettingsPeer (account settings)
 *   setUserSetting()
 *   setUserSettingArray($settings)
 *
 *   getUserDetails()                Get UsersPeer record for an authorized user
 *   getLocalPrefs()
 *
 *   isAdministrator()
 *
 *   signIn()
 *   signOut()
 *
 *   changePassword($username, $raw_password)
 *   getSaltyHashedPassword($raw_password)
 *   
 *   redirectToLogin($options = array())      Redirect unauthenticated user to login page
 *
 * 
 * @author     <NAME>
 */

class rtkUser extends sfBasicSecurityUser
{
  protected
    $localPrefs = null;
  
  // The "Remember me" cookie name and lifetime in seconds.
  const COOKIE_EXPIRE = 31536000; // 60*60*24*365

  const CREDENTIAL_ADMIN  = 'admin';
  const CREDENTIAL_MEMBER = 'member';

  // Misc. states that do not need database permanence
  const IS_RESTUDY_SESSION = 'study.restudy.start';

  // misc. session attributes 
  const KNOWN_KANJI = 'kanji.known';

  /**
   * 
   */
  public function initialize(sfEventDispatcher $dispatcher, sfStorage $storage, $options = array())
  {
    parent::initialize($dispatcher, $storage, $options);

    // sign in unauthenticated user if a "remember me" cookie exists
    if (!$this->isAuthenticated())
    {
      if ($cookieData = sfContext::getInstance()->getRequest()->getCookie(sfConfig::get('app_cookie_name')))
      {
        $value = unserialize(base64_decode($cookieData));
        $username = $value[0];
        $saltyPassword = $value[1];
        
        // sign in user if user is valid and password from cookie matches the one in database
        $user = UsersPeer::getUser($username);
        if ($user && ($saltyPassword == $user['password']) )
        {
          $this->signIn($user);
        }
      }
    }
    
    // session duration preferences
    $this->localPrefs = new LocalPrefs($this);
  }

  /**
   * Getters for user attributes.
   */
  public function getUserName()
  {
    return $this->getAttribute('username', '');
  }

  // returns int | null
  public function getUserId()
  {
    $uid = $this->getAttribute('userid', null);
    return null !== $uid ? (int)$uid : null;
  }

  public function getUserTimeZone()
  {
    return $this->getAttribute('usertimezone', null);
  }

  public function getUserSequence()
  {
    return $this->getAttribute('usersequence', 0);
  }

  /**
   * Known kanji: cache a string of al the kanji known by user
   *
   * @param bool   $refresh   set true to refresh the cache  
   */
  public function getUserKnownKanji(bool $refresh = false)
  {

    if (!$refresh && null !== ($knownKanji = $this->getAttribute(self::KNOWN_KANJI, null))) {
      return $knownKanji;
    }
    
    $knownKanji = ReviewsPeer::getKnownKanji($this->getUserId());
    $this->setUserKnownKanji($knownKanji);

    return $knownKanji;
  }

  public function setUserKnownKanji(string $kanjis)
  {
    $this->setAttribute(self::KNOWN_KANJI, $kanjis);
  }

  // [listener] atm the query is fast, so no add/delete/etc, just invalidate the cached data
  static public function eventUpdateUserKnownKanji(sfEvent $event)
  {
    $user = sfContext::getInstance()->getUser();
    $user->getAttributeHolder()->remove(self::KNOWN_KANJI);<commit_msg>Fix php error "type hint parameter can only used NULL as default value". Oh well.<commit_after><?php
/**
 * rtkUser adds utility methods to the sfUser class.
 * 
 * Methods
 *   getUserId()
 *   getUserName()
 *   getUserTimeZone()
 *   getUserSequence()
 *
 *   getUserSetting()                Proxy and cache for UsersSettingsPeer (account settings)
 *   setUserSetting()
 *   setUserSettingArray($settings)
 *
 *   getUserDetails()                Get UsersPeer record for an authorized user
 *   getLocalPrefs()
 *
 *   isAdministrator()
 *
 *   signIn()
 *   signOut()
 *
 *   changePassword($username, $raw_password)
 *   getSaltyHashedPassword($raw_password)
 *   
 *   redirectToLogin($options = array())      Redirect unauthenticated user to login page
 *
 * 
 * @author     <NAME>
 */

class rtkUser extends sfBasicSecurityUser
{
  protected
    $localPrefs = null;
  
  // The "Remember me" cookie name and lifetime in seconds.
  const COOKIE_EXPIRE = 31536000; // 60*60*24*365

  const CREDENTIAL_ADMIN  = 'admin';
  const CREDENTIAL_MEMBER = 'member';

  // Misc. states that do not need database permanence
  const IS_RESTUDY_SESSION = 'study.restudy.start';

  // misc. session attributes 
  const KNOWN_KANJI = 'kanji.known';

  /**
   * 
   */
  public function initialize(sfEventDispatcher $dispatcher, sfStorage $storage, $options = array())
  {
    parent::initialize($dispatcher, $storage, $options);

    // sign in unauthenticated user if a "remember me" cookie exists
    if (!$this->isAuthenticated())
    {
      if ($cookieData = sfContext::getInstance()->getRequest()->getCookie(sfConfig::get('app_cookie_name')))
      {
        $value = unserialize(base64_decode($cookieData));
        $username = $value[0];
        $saltyPassword = $value[1];
        
        // sign in user if user is valid and password from cookie matches the one in database
        $user = UsersPeer::getUser($username);
        if ($user && ($saltyPassword == $user['password']) )
        {
          $this->signIn($user);
        }
      }
    }
    
    // session duration preferences
    $this->localPrefs = new LocalPrefs($this);
  }

  /**
   * Getters for user attributes.
   */
  public function getUserName()
  {
    return $this->getAttribute('username', '');
  }

  // returns int | null
  public function getUserId()
  {
    $uid = $this->getAttribute('userid', null);
    return null !== $uid ? (int)$uid : null;
  }

  public function getUserTimeZone()
  {
    return $this->getAttribute('usertimezone', null);
  }

  public function getUserSequence()
  {
    return $this->getAttribute('usersequence', 0);
  }

  /**
   * Known kanji: cache a string of al the kanji known by user
   *
   * @param bool   $refresh   set true to refresh the cache  
   */
  public function getUserKnownKanji($refresh = false)
  {

    if (!$refresh && null !== ($knownKanji = $this->getAttribute(self::KNOWN_KANJI, null))) {
      return $knownKanji;
    }
    
    $knownKanji = ReviewsPeer::getKnownKanji($this->getUserId());
    $this->setUserKnownKanji($knownKanji);

    return $knownKanji;
  }

  public function setUserKnownKanji(string $kanjis)
  {
    $this->setAttribute(self::KNOWN_KANJI, $kanjis);
  }

  // [listener] atm the query is fast, so no add/delete/etc, just invalidate the cached data
  static public function eventUpdateUserKnownKanji(sfEvent $event)
  {
    $user = sfContext::getInstance()->getUser();
    $user->getAttributeHolder()->remove(self::KNOWN_KANJI);
{'fraction_non_alphanumeric': 0.12849081174938504, 'fraction_numerical': 0.006366661843437997, 'mean_word_length': 3.09478672985782, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2615289', 'n_tokens_mistral': 2214, 'n_tokens_neox': 2112, 'n_words': 522}