static function info_block_get_metadata($block, $item) { $link = ORM::factory("bitly_link")->where("item_id", "=", $item->id)->find(); if ($link->loaded()) { $info = $block->content->metadata; $info["bitly_url"] = array("label" => t("bit.ly url:"), "value" => bitly::url($link->hash)); $block->content->metadata = $info; } }
/** * Shorten a G3 item's link and display the result in a status message. * @param int $item_id */ public function shorten($item_id) { // Prevent Cross Site Request Forgery access::verify_csrf(); $item = ORM::factory("item", $item_id); // Ensure user has permission access::required("view", $item); access::required("edit", $item); // Get the item's URL and shorten it $short_url = bitly::shorten_url($item_id); // Redirect back to the item url::redirect(url::abs_site($item->relative_url_cache)); }
/** * Shorten a G3 item's link and display the result in a status message. * @param int $item_id */ public function shorten($item_id) { // Prevent Cross Site Request Forgery access::verify_csrf(); $item = ORM::factory("item", $item_id); // Ensure user has permission access::required("view", $item); access::required("edit", $item); // Shorten the item's URL $short_url = bitly::shorten_url($item_id); if ($short_url) { message::success("Item URL shortened to {$short_url}"); } else { message::error("Unable to shorten " . url::abs_site($item->relative_url_cache)); } // Redirect back to the item url::redirect(url::abs_site($item->relative_url_cache)); }
/** * Get tweet form * @param object $item * @return Forge */ static function get_tweet_form($item) { $long_url = url::abs_site($item->relative_url_cache); // Check for saved tweets for this user and item $saved = self::get_failed($item->id); if ($saved) { $default_tweet = $saved->tweet; $tweet_id = $saved->id; } else { $default_tweet = module::get_var("twitter", "default_tweet"); $tweet_id = 0; } $tweet = preg_replace("/%type/", $item->type, $default_tweet); $tweet = preg_replace("/%title/", $item->title, $tweet); $tweet = preg_replace("/%description/", $item->description, $tweet); // If bit.ly module's enabled, get the item's URL and shorten it if (!empty($item->id) && module::is_active("bitly") && module::get_var("twitter", "shorten_urls")) { $url = bitly::shorten_url($item->id); } else { $url = url::abs_site($item->relative_url_cache); } // Truncate the default tweet if it's too long $url_length = strlen($url) + 1; $tweet_length = strlen($tweet); if ($tweet_length + $url_length > self::$character_count) { $trim_pos = 0 - ($tweet_length + $url_length - 140); $tweet = substr($tweet, 0, $trim_pos); } $tweet = $tweet . ' ' . $url; $form = new Forge("twitter/tweet/{$item->id}", "", "post", array("id" => "g-twitter-tweet-form")); $group = $form->group("twitter_message")->label(t("Compose Tweet")); $group->textarea("tweet")->value($tweet)->rules("required")->error_messages("required", t("Your tweet cannot be empty!"))->id("g-tweet"); $group->hidden("tweet_id")->value($tweet_id)->id("tweet_id"); $group->checkbox("attach_image")->label(t("Attach resize image to tweet (reduces available chars)?"))->value(1)->id("g-attach_image"); $form->submit("")->value(t("Tweet")); return $form; }
/** * bit.ly module's settings * @todo Create/get and display the shortened value for this Gallery's root album (home page) */ public function index() { $form = bitly::get_configure_form(); $login = module::get_var("bitly", "login"); $api_key = module::get_var("bitly", "api_key"); $domain = module::get_var("bitly", "domain"); $valid_config = false; if (request::method() == "post") { access::verify_csrf(); if ($form->validate()) { $new_login = $form->configure_bitly->login->value; $new_key = $form->configure_bitly->api_key->value; $new_domain = $form->configure_bitly->domain->value; module::set_var("bitly", "login", $new_login); module::set_var("bitly", "api_key", $new_key); module::set_var("bitly", "domain", $new_domain); if (!bitly::check_config()) { url::redirect("admin/bitly"); } else { if ($login && !$new_login) { message::success(t("Your bit.ly login has been cleared.")); } else { if ($login && $new_login && $login != $new_login) { message::success(t("Your bit.ly login has been changed.")); } else { if (!$login && $new_login) { message::success(t("Your bit.ly login has been saved.")); } } } if ($api_key && !$new_key) { message::success(t("Your bit.ly API key has been cleared.")); } else { if ($api_key && $new_key && $api_key != $new_key) { message::success(t("Your bit.ly API key has been changed.")); } else { if (!$api_key && $new_key) { message::success(t("Your bit.ly API key has been saved.")); } } } if ($domain && $new_domain && $domain != $new_domain) { message::success(t("Your preferrend bit.ly domain has been changed.")); } else { if (!$domain && $new_domain) { message::success(t("Your preferred bit.ly domain has been saved.")); } } log::success("bitly", t("bit.ly login changed to %new_login", array("new_login" => $new_login))); log::success("bitly", t("bit.ly API key changed to %new_key", array("new_key" => $new_key))); !$new_login || !$new_key ? $valid_config = false : ($valid_config = true); } } } $view = new Admin_View("admin.html"); $view->page_title = t("bit.ly url shortner"); $view->content = new View("admin_bitly.html"); $view->content->login = $form->configure_bitly->login->value; $view->content->api_key = $form->configure_bitly->api_key->value; $view->content->domain = $form->configure_bitly->domain->value; $view->content->form = $form; $link = ORM::factory("bitly_link")->where("item_id", "=", 1)->find(); if ($link->loaded()) { $view->content->g3_url = bitly::url($link->hash); } else { if ($valid_config && !empty($login) && !empty($api_key) && !empty($domain)) { $view->content->g3_url = bitly::shorten_url(1); } } print $view; }
/** * Manipulates posted form data for insertion into database * * @param mixed $val this elements posted form data * @param array $data posted form data * * @return mixed */ public function storeDatabaseFormat($val, $data) { /* $$$ hugh - added 'normalization' of links, to add http:// if no :// in the link. * not sure if we really want to do it here, or only when rendering? * $$$ hugh - quit normalizing links. */ $params = $this->getParams(); if (is_array($val)) { if ($params->get('use_bitly')) { require_once JPATH_SITE . '/components/com_fabrik/libs/bitly/bitly.php'; $login = $params->get('bitly_login'); $key = $params->get('bitly_apikey'); $bitly = new bitly($login, $key); } foreach ($val as $key => &$v) { if (is_array($v)) { if ($params->get('use_bitly')) { /* bitly will return an error if you try and shorten a shortened link, * and the class file we are using doesn't check for this */ if (!strstr($v['link'], 'bit.ly/') && $v['link'] !== '') { $v['link'] = (string) $bitly->shorten($v['link']); } } } else { if ($key == 'link') { $v = FabrikString::encodeurl($v); } // Not in repeat group if ($key == 'link' && $params->get('use_bitly')) { if (!strstr($v, 'bit.ly/') && $v !== '') { $v = (string) $bitly->shorten($v); } } } } } else { if (json_decode($val)) { return $val; } } $return = json_encode($val); return $return; }
/** * Tweet on save of item */ public function hookSaveItem(&$pa_params) { $t_subject = $pa_params['instance']; // get instance from params $bitly = new bitly('collectiveaccess', 'R_8a0ecd6ea746c58f787d6769329b9976'); // check access $va_access_values = $this->opo_config->getList($t_subject->tableName() . '_tweet_when_access'); if (is_array($va_access_values) && sizeof($va_access_values)) { if (!in_array($t_subject->get('access'), $va_access_values)) { // Skip record because it is not publicly accessible return $pa_params; } } // check update threshold (prevents repeated tweeting when a record is repeatedly saved over a short period of time) if (($vn_threshold = $this->opo_config->get($t_subject->tableName() . "_tweet_update_threshold")) <= 0) { // seconds $vn_threshold = 3600; // default to one hour if no threshold is specified } $vb_access_changed = $t_subject->get('access') != $this->opn_old_access_value; $this->opn_old_access_value = null; if (!$vb_access_changed && time() - $this->opn_last_update_timestamp < $vn_threshold) { return $pa_params; } // Get Twitter token. If it doesn't exist silently skip posting. if ($o_token = @unserialize(file_get_contents(__CA_APP_DIR__ . '/tmp/twitter.token'))) { try { $o_twitter = new Zend_Service_Twitter(array('consumerKey' => $this->opo_config->get('consumer_key'), 'consumerSecret' => $this->opo_config->get('consumer_secret'), 'username' => $this->opo_config->get('twitter_username'), 'accessToken' => $o_token)); // Post to Twitter $vn_id = $t_subject->getPrimaryKey(); $vs_url = $this->opo_config->get($t_subject->tableName() . '_public_url') . $vn_id; $vs_url_bitly = $bitly->shorten($vs_url); $vs_tweet = $this->opo_config->get($pa_params['is_insert'] ? $t_subject->tableName() . '_new_message' : $t_subject->tableName() . '_updated_message'); if ($vs_tweet) { // substitute tags $vs_tweet = caProcessTemplate($vs_tweet, array('BITLY_URL' => $vs_url_bitly, 'URL' => $vs_url, 'ID' => $vn_id), array('getFrom' => $t_subject, 'delimiter' => ', ')); if (mb_strlen($vs_tweet) > 140) { $vs_tweet = mb_substr($vs_tweet, 0, 140); } $o_response = $o_twitter->status->update($vs_tweet); } } catch (Exception $e) { // Don't post error to user - Twitter failing is not a critical error // But let's put it in the event log so you have some chance of knowing what's going on //print "Post to Twitter failed: ".$e->getMessage(); $o_log = new Eventlog(); $o_log->log(array('SOURCE' => 'twitter plugin', 'MESSAGE' => _t('Post to Twitter failed: %1', $e->getMessage()), 'CODE' => 'ERR')); } } return $pa_params; }
/** * manupulates posted form data for insertion into database * @param mixed thie elements posted form data * @param array posted form data */ function storeDatabaseFormat($val, $data) { // $$$ hugh - added 'normalization' of links, to add http:// if no :// in the link. // not sure if we really want to do it here, or only when rendering? // $$$ hugh - quit normalizing links. $return = ''; $params = $this->getParams(); if (is_array($val)) { if ($params->get('use_bitly')) { require_once JPATH_SITE . DS . 'components' . DS . 'com_fabrik' . DS . 'libs' . DS . 'bitly' . DS . 'bitly.php'; $login = $params->get('bitly_login'); $key = $params->get('bitly_apikey'); $bitly = new bitly($login, $key); } foreach ($val as $key => &$v) { if (is_array($v)) { if ($params->get('use_bitly')) { // bitly will return an error if you try and shorten a shortened link, // and the class file we are using doesn't check for this if (!strstr($v['link'], 'bit.ly/') && $v['link'] !== '') { $v['link'] = $bitly->shorten($v['link']); } } /*$return .= implode(GROUPSPLITTER2, $v); $return .= GROUPSPLITTER;*/ } else { // not in repeat group if ($key == 'link' && $params->get('use_bitly')) { if (!strstr($v, 'bit.ly/') && $v !== '') { $v = $bitly->shorten($v); } } } } } else { $return = $val; } $return = json_encode($val); return $return; }
static function install() { Database::instance()->query("CREATE TABLE {bitly_links} (\n `id` int(9) NOT NULL AUTO_INCREMENT,\n `item_id` int(9) NOT NULL,\n `hash` char(6) NOT NULL,\n `global_hash` char(6) NOT NULL,\n PRIMARY KEY (`id`))\n DEFAULT CHARSET=utf8;"); module::set_version("bitly", 1); bitly::check_config(); }