Exemplo n.º 1
0
 /**
  * Post the title of Question or Answer to Twitter
  * Usually this method is called as a shutdown_function
  * @todo if space allows add "prefixes" to Tweets
  * Prefixes will be strings, in translation...
  * 
  * @todo if space allows also add "via @ourname" to tweet if the
  * value of TWITTER_USERNAME exists if setting
  *
  * @param \Lampcms\Twitter $oTwitter
  * @param \Lampcms\Bitly $oBitly
  * @param object $Resource object of type Question or Answer
  * @return mixed null if exception was caught or array returned
  * by Twitter API
  */
 public function post(\Lampcms\Twitter $oTwitter, \Lampcms\Bitly $oBitly, $Resource)
 {
     d('cp');
     if (!$Resource instanceof \Lampcms\Question && !$Resource instanceof \Lampcms\Answer) {
         e('Resource not Question and not Answer');
         return;
     }
     $ret = null;
     /**
      * $title is already guaranteed to be
      * in utf-8
      */
     $title = $Resource['title'];
     /**
      * Short url from bit.ly is guaranteed
      * to be in utf-8
      */
     $short = $oBitly->getShortUrl($Resource->getUrl());
     /**
      * Our own url is in utf8 unless...
      * Unless this site is on some weird international
      * domain name that includes non-utf8 chars
      * This is super unlikely
      * We can assume that all components of
      * the tweet is already in utf-8
      */
     $url = $short ? $short : $Resource->getUrl(true);
     /**
      * Test what the length of tweet will be
      * if we concatinate title + space + url
      * 
      * @var int
      */
     $testLength = \mb_strlen($url . ' ' . $title, 'utf-8');
     if ($testLength > 140) {
         d('need to shorten title');
         $title = Utf8String::factory($title, 'utf-8', true)->truncate(139 - \mb_strlen($url, 'utf-8'))->valueOf();
         $text = $title . ' ' . $url;
     } else {
         $text = $title . ' ' . $url;
     }
     d('going to tweet this text: ' . $text);
     try {
         $ret = $oTwitter->postMessage($text);
     } catch (\Exception $e) {
         e('Tweet not sent because of exception: ' . $e->getMessage() . ' in file: ' . $e->getFile() . ' on line: ' . $e->getLine());
     }
     return $ret;
 }
Exemplo n.º 2
0
 /**
  * (non-PHPdoc)
  * @see Lampcms.SubmittedAnswer::getBody()
  */
 public function getBody()
 {
     if (!isset($this->Body)) {
         $this->Body = Utf8String::factory($this->aData['qbody']);
     }
     return $this->Body;
 }
Exemplo n.º 3
0
 /**
  *
  * Get value of requested param converted
  * to Utf8String object
  *
  * @param string $name
  * @param mixed $default fallback value in case
  * the param $name does not exist if Request
  *
  * @return object of type Utf8String representing the value
  * of requested param
  */
 public function getUTF8($name, $default = null)
 {
     if (empty($this->aUTF8[$name])) {
         $res = $this->get($name, 's', $default);
         $ret = Utf8String::factory($res);
         $this->aUTF8[$name] = $ret;
     }
     return $this->aUTF8[$name];
 }
Exemplo n.º 4
0
 /**
  *
  * Set tags for this question
  * It will also update "a_edited" array
  * to record the retag action, records
  * user who retagged, and "Retag" as reason for edit
  * Will also update lastModified
  *
  * @param User $user object User who retagged this question
  * @param array $tags array of tags
  */
 public function retag(User $user, array $tags)
 {
     parent::offsetSet('a_tags', $tags);
     parent::offsetSet('tags_html', \tplQtags::loop($tags, false));
     $b = $this->offsetGet('b');
     d('b: ' . $b);
     $oHtmlParser = \Lampcms\String\HTMLStringParser::factory(Utf8String::factory($b, 'utf-8', true));
     $body = $oHtmlParser->unhilight()->hilightWords($tags)->valueOf();
     $this->offsetSet('b', $body);
     $this->setEdited($user, 'Retagged')->touch();
     return $this;
 }
 /**
  * Returns Tags which is
  * object of type Utf8String
  *
  * (non-PHPdoc)
  * @see Lampcms.SubmittedQuestion::getTags()
  */
 public function getUtf8Tags()
 {
     if (!isset($this->Tags)) {
         $this->Tags = Utf8String::factory($this->aData['tags']);
     }
     return $this->Tags;
 }
Exemplo n.º 6
0
 /**
  * Checks in username of twitter user
  * already exists in our regular USERS table
  * and if it does then prepends the @ to the username
  * otherwise returns twitter username
  *
  * The result is that we will use the value of
  * Twitter username as our username OR the @username
  * if username is already taken
  *
  * @todo change this to use MONGO USERS and use something like
  * $any
  *
  * @return string the value of username that will
  * be used as our own username
  *
  */
 public function makeUsername($displayName, $isUtf8 = false)
 {
     d('going to auto_create username based on displayName: ' . $displayName);
     /**
      * Make 100% sure that displayName is in UTF8 encoding
      * Commenting this out for now since it was causing
      * a problem once.
      * So for now we going to trust that Facebook give us results
      * as a valid UTF-8 String
      */
     if (!$isUtf8) {
         $displayName = Utf8String::factory($displayName)->valueOf();
     }
     $coll = $this->Registry->Mongo->USERS;
     $res = null;
     $username = null;
     $aUsernames = array(preg_replace('/\\s+/', '_', $displayName), preg_replace('/\\s+/', '', $displayName), preg_replace('/\\s+/', '.', $displayName), preg_replace('/\\s+/', '-', $displayName));
     $aUsernames = \array_unique($aUsernames);
     d('$aUsernames: ' . print_r($aUsernames, 1));
     for ($i = 0; $i < count($aUsernames); $i++) {
         $name = \mb_strtolower($aUsernames[$i], 'utf-8');
         $res = $coll->findOne(array('username_lc' => $name));
         d('$res: ' . $res);
         if (empty($res)) {
             $username = $aUsernames[$i];
             break;
         }
     }
     /**
      * If still could not find username then
      * use brute force and try appending numbers
      * to username untill succeed
      */
     if (null === $username) {
         $i = 1;
         do {
             $name = \mb_strtolower($aUsernames[0], 'utf-8') . $i;
             $res = $coll->findOne(array('username_lc' => $name));
             if (empty($res)) {
                 $username = $aUsernames[0] . $i;
             }
             d('$res: ' . $res);
         } while (null === $username);
     }
     return $username;
 }
 public function getHtml()
 {
     $edit = '';
     $lastActive = $this->User['i_lm_ts'];
     $lastActive = !empty($lastActive) ? $lastActive : $this->User['i_reg_ts'];
     $pp = $this->User->getProfitPoint();
     d('pp: ' . $pp);
     $uid = $this->User->getUid();
     $isSameUser = $this->Registry->Viewer->getUid() === $uid;
     if ($isSameUser || $this->Registry->Viewer->isModerator()) {
         $edit = '<div class="fl middle"><span class="icoc key">&nbsp;</span><a href="/editprofile/' . $uid . '" class="edit middle">Edit profile</a></div>';
     }
     $desc = \trim($this->User['description']);
     $desc = empty($desc) ? '' : Utf8String::factory($desc, 'utf-8', true)->linkify()->valueOf();
     $vars = array('editLink' => $edit, 'username' => $this->User->username, 'avatar' => $this->User->getAvatarImgSrc(), 'profitpoint' => $pp, 'name' => $this->User->getDisplayName(), 'genderLabel' => 'Gender', 'gender' => $this->getGender(), 'since' => date('F j, Y', $this->User->i_reg_ts), 'lastActivity' => TimeAgo::format(new \DateTime(date('r', $lastActive))), 'website' => $this->User->getUrl(), 'twitter' => '<div id="my_tw">' . $this->getTwitterAccount($isSameUser) . '</div>', 'age' => $this->User->getAge(), 'facebook' => '<div id="my_fb">' . $this->getFacebookAccount($isSameUser) . '</div>', 'tumblr' => '<div id="my_tm">' . $this->getTumblrAccount($isSameUser) . '</div>', 'blogger' => '<div id="my_bg">' . $this->getBloggerAccount($isSameUser) . '</div>', 'linkedin' => '<div id="my_li">' . $this->getLinkedInAccount($isSameUser) . '</div>', 'location' => $this->User->getLocation(), 'description' => \wordwrap($desc, 50), 'editRole' => Usertools::getHtml($this->Registry, $this->User), 'followButton' => $this->makeFollowButton(), 'followers' => ShowFollowers::factory($this->Registry)->getUserFollowers($this->User), 'following' => ShowFollowers::factory($this->Registry)->getUserFollowing($this->User));
     return \tplUserInfo::parse($vars);
 }
Exemplo n.º 8
0
 public function testGetWordsCount()
 {
     $o = Utf8String::factory(self::UTF8_RUSSIAN);
     $this->assertEquals(3, $o->getWordsCount());
 }
Exemplo n.º 9
0
 /**
  * Get clean UTF8String object representing
  * trimmed and clean of html tags
  *
  * @param string $string
  * @return object of type UTF8String
  */
 protected function getClean($string)
 {
     if (empty($string)) {
         return Utf8String::factory('', 'ascii', true);
     }
     return Utf8String::factory($string)->trim()->stripTags();
 }
Exemplo n.º 10
0
 /**
  * Removes the tag name from the array of a_f_t
  * of User object and increases the i_f_t by one
  * if USER already follow this tag
  * also decreases the i_flwrs in QUESTION_TAGS collection
  * by one for this tag
  *
  *
  * @param User $User
  * @param string $tag
  * @throws \InvalidArgumentException if $tag is not a string
  */
 public function unfollowTag(User $User, $tag)
 {
     if (!is_string($tag)) {
         throw new \InvalidArgumentException('$tag must be a string');
     }
     $tag = Utf8String::factory($tag)->toLowerCase()->stripTags()->trim()->valueOf();
     $aFollowed = $User['a_f_t'];
     d('$aFollowed: ' . print_r($aFollowed, 1));
     if (false !== ($key = array_search($tag, $aFollowed))) {
         d('cp unsetting key: ' . $key);
         array_splice($aFollowed, $key, 1);
         $User['a_f_t'] = $aFollowed;
         $User->save();
         $this->Registry->Mongo->QUESTION_TAGS->update(array('tag' => $tag), array('$inc' => array('i_flwrs' => -1)));
         $this->Registry->Dispatcher->post($User, 'onTagUnfollow', array('tag' => $tag));
     } else {
         d('tag ' . $tag . ' is not among the followed tags of this userID: ' . $User->getUid());
     }
     return $this;
 }