Issue #10730: Use crypto_api for generating nonces and improve hashing
[mantis/radio.git] / core / twitter_api.php
blob29874027940ee526381e37839772dc5cc164b3d6
1 <?php
2 # MantisBT - A PHP based bugtracking system
4 # MantisBT is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 2 of the License, or
7 # (at your option) any later version.
9 # MantisBT is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with MantisBT. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Twitter API
20 * @package CoreAPI
21 * @subpackage TwitterAPI
22 * @copyright Copyright (C) 2000 - 2002 Kenzaburo Ito - kenito@300baud.org
23 * @copyright Copyright (C) 2002 - 2010 MantisBT Team - mantisbt-dev@lists.sourceforge.net
24 * @link http://www.mantisbt.org
26 * @uses bug_api.php
27 * @uses category_api.php
28 * @uses config_api.php
29 * @uses constant_inc.php
30 * @uses database_api.php
31 * @uses lang_api.php
32 * @uses news_api.php
33 * @uses project_api.php
34 * @uses user_api.php
35 * @uses utility_api.php
38 require_api( 'bug_api.php' );
39 require_api( 'category_api.php' );
40 require_api( 'config_api.php' );
41 require_api( 'constant_inc.php' );
42 require_api( 'database_api.php' );
43 require_api( 'lang_api.php' );
44 require_api( 'news_api.php' );
45 require_api( 'project_api.php' );
46 require_api( 'user_api.php' );
47 require_api( 'utility_api.php' );
49 $g_twitter_enabled = null;
51 /**
52 * Checks if twitter is used for the current installation.
53 * returns true for enabled, false otherwise.
55 * @return true: twitter enabled, false: otherwise.
56 * @access public
58 function twitter_enabled() {
59 global $g_twitter_enabled;
61 if( null === $g_twitter_enabled ) {
62 $g_twitter_enabled = !is_blank( config_get( 'twitter_username' ) );
65 if( $g_twitter_enabled && !function_exists( 'curl_init' ) ) {
66 trigger_error( ERROR_TWITTER_NO_CURL_EXT, ERROR );
69 return $g_twitter_enabled;
72 /**
73 * Posts a twitter update when a bug is resolved.
75 * @param $p_bug_id The bug id that was resolved.
76 * @access public
78 function twitter_issue_resolved( $p_bug_id ) {
79 if( !twitter_enabled() ) {
80 return true;
83 $t_bug = bug_get( $p_bug_id, false );
85 # Do not twitter except fixed issues
86 if( $t_bug->resolution < config_get( 'bug_resolution_fixed_threshold' ) ||
87 $t_bug->resolution >= config_get( 'bug_resolution_not_fixed_threshold' ) ) {
88 return true;
91 # Do not twitter private bugs.
92 if( $t_bug->view_state != VS_PUBLIC ) {
93 return true;
96 # Do not twitter bugs belonging to private projects.
97 if( VS_PRIVATE == project_get_field( $t_bug->project_id, 'view_state' ) ) {
98 return true;
101 $c_bug_id = db_prepare_int( $p_bug_id );
103 if( is_blank( $t_bug->fixed_in_version ) ) {
104 $t_message = sprintf( lang_get( 'twitter_resolved_no_version' ), $c_bug_id, category_full_name( $t_bug->category_id,
106 /* include project */
107 false ), $t_bug->summary, user_get_name( $t_bug->handler_id ) );
108 } else {
109 $t_message = sprintf( lang_get( 'twitter_resolved' ), $c_bug_id, category_full_name( $t_bug->category_id,
111 /* include project */
112 false ), $t_bug->summary, user_get_name( $t_bug->handler_id ), $t_bug->fixed_in_version );
115 return twitter_update( $t_message );
119 * Posts a twitter update when a news entry is submitted.
121 * @param $p_news_id The newly posted news id.
122 * @access public
124 function twitter_news( $p_news_id ) {
125 if( !twitter_enabled() ) {
126 return true;
129 $t_news_view_state = news_get_field( $p_news_id, 'view_state' );
130 if( VS_PUBLIC != $t_news_view_state ) {
131 return true;
134 $t_news_project_id = news_get_field( $p_news_id, 'project_id' );
135 if( $t_news_project_id != ALL_PROJECTS ) {
136 $t_project_view_state = project_get_field( $t_news_project_id, 'view_state' );
137 if( VS_PUBLIC != $t_project_view_state ) {
138 return true;
142 $t_news_headline = news_get_field( $p_news_id, 'headline' );
144 return twitter_update( $t_news_headline );
148 * Posts an update to twitter
150 * @param $p_message The message to post.
151 * @access private
153 function twitter_update( $p_message ) {
154 if( !twitter_enabled() ) {
155 return true;
158 if( is_blank( $p_message ) ) {
159 return true;
162 # don't prepare the string, otherwise it will be escaped twice once by MantisBT and once by Twitter
163 $c_message = $p_message;
165 // Set username and password
166 $t_username = config_get( 'twitter_username' );
167 $t_password = config_get( 'twitter_password' );
169 // The twitter API address
170 $t_url = 'http://twitter.com/statuses/update.xml';
172 // Set up and execute the curl process
173 $t_curl = curl_init();
175 curl_setopt( $t_curl, CURLOPT_URL, $t_url );
176 curl_setopt( $t_curl, CURLOPT_CONNECTTIMEOUT, 2 );
177 curl_setopt( $t_curl, CURLOPT_RETURNTRANSFER, 1 );
178 curl_setopt( $t_curl, CURLOPT_POST, 1 );
179 curl_setopt( $t_curl, CURLOPT_POSTFIELDS, 'status=' . $c_message );
180 curl_setopt( $t_curl, CURLOPT_USERPWD, $t_username . ':' . $t_password );
182 $t_buffer = curl_exec( $t_curl );
184 curl_close( $t_curl );
186 return !is_blank( $t_buffer );