/** * 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; }
/** * * 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]; }
public static function factory(Utf8String $str) { return new self($str->toLowerCase()->trim()->valueOf()); }
/** * Factory method * Makes the object is this class * and load the html string, first wraps the html * string into the <div class="newsItem"> * * @param object of type Utf8String $sHtml html string to load * usually this is the feed item from rss feed. * by being an object of type Utf8String it's guaranteed * to be an already in utf-8 charset * * @return object of this class * * @throws LampcmsDevException is unable to load the string */ public static function loadFeedItem(Utf8String $oHtml, $sBaseUri = '', $bAddNoFollow = true, $parseCodeTags = true) { $oDom = new self('1.0', 'utf-8'); $oDom->encoding = 'UTF-8'; $oDom->preserveWhiteSpace = true; $oDom->recover = true; $oDom->setNofollow($bAddNoFollow); $sHtml = $oHtml->valueOf(); /** * @todo * maybe we should add class to this div and * then in the getFeedItem() don't remove the div at all, * so it will always be part of feed item's html, * it's just going to wrap the entire item. * So when we add item to a page we know it will always be wrapped * in this additional div * */ /** * Extremely important to add the * <META CONTENT="text/html; charset=utf-8"> * This is the ONLY way to tell the DOM (more spefically * the libxml) that input is in utf-8 encoding * Whithout this the DOM will assume that input is in the * default ISO-8859-1 format and then * will try to recode it to utf8 * essentially it will do its own conversion to utf8, * messing up the string because it's already in utf8 and does not * need converting * * IMPORTANT: we are also wrapping the whole string in <div> * so that it will be easy to get back just the contents of * the first div * */ $sHtml = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <head> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8"> </head> <body><div>' . $sHtml . '</div></body></html>'; $ER = error_reporting(0); if (false === @$oDom->loadHTML($sHtml)) { throw new DevException('Error. Unable to load html string: ' . $sHtml); } error_reporting($ER); /** * If $sBaseUrl begins with http * then set the $this->baseUri value to this value * and make sure it always ends with forward slash */ if (!empty($sBaseUri) && 'http' === substr($sBaseUri, 0, 4) && strlen($sBaseUri) > 12) { $oDom->baseUri = rtrim($sBaseUri, '/') . '/'; } $oDom->setRelNofollow(); if ($parseCodeTags) { $oDom->parseCodeTags(); } $oDom->fixImgBaseUri(); //->getImages(); return $oDom; }
/** * (non-PHPdoc) * @see Lampcms.SubmittedAnswer::getBody() */ public function getBody() { if (!isset($this->Body)) { $this->Body = Utf8String::factory($this->aData['qbody']); } return $this->Body; }
/** * * 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; }
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"> </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); }
/** * 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; }
/** * Sets an inflected word in the cache. * * @param string $singular The singular form of the word. * @param string $plural The plural form of the word. If omitted, it is assumed the singular and plural are identical. * * @return void * * @since 2.0 */ private function setCache($singular, $plural = null) { $singular = Utf8String::strtolower($singular); if ($plural === null) { $plural = $singular; } else { $plural = Utf8String::strtolower($plural); } $this->cache[$singular] = $plural; }
/** * 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::stringFactory('', 'ascii', true); } return Utf8String::stringFactory($string)->trim()->stripTags(); }
/** * Factory method. This class should be instantiated * ONLY through this method * * @param Utf8String $Tags * * @internal param \Lampcms\of $object type Utf8string $Tags * @return object of this class */ public static function factory(Utf8String $Tags) { $str = $Tags->toLowerCase()->trim()->valueOf(); return new self($str); }
public function testStripLow() { $s = "String with tab\tand \nNew Line1\nNew Line2"; $this->assertEquals($s, Utf8String::stripLow($s)); }
/** * 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; }