PageRenderTime 26ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/application/controllers/admin/messages.php

http://github.com/ushahidi/Ushahidi_Web
PHP | 407 lines | 279 code | 48 blank | 80 comment | 28 complexity | 4a182ecc9729de55202f71751201dced MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * Messages Controller.
  4. *
  5. * PHP version 5
  6. * LICENSE: This source file is subject to LGPL license
  7. * that is available through the world-wide-web at the following URI:
  8. * http://www.gnu.org/copyleft/lesser.html
  9. * @author Ushahidi Team <team@ushahidi.com>
  10. * @package Ushahidi - http://source.ushahididev.com
  11. * @subpackage Admin
  12. * @copyright Ushahidi - http://www.ushahidi.com
  13. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License (LGPL)
  14. */
  15. class Messages_Controller extends Admin_Controller {
  16. public function __construct()
  17. {
  18. parent::__construct();
  19. $this->template->this_page = 'messages';
  20. // If user doesn't have access, redirect to dashboard
  21. if ( ! $this->auth->has_permission("messages"))
  22. {
  23. url::redirect(url::site().'admin/dashboard');
  24. }
  25. }
  26. /**
  27. * Lists the messages.
  28. * @param int $service_id
  29. */
  30. public function index($service_id = 1)
  31. {
  32. // If a table prefix is specified
  33. $db_config = Kohana::config('database.default');
  34. $table_prefix = $db_config['table_prefix'];
  35. $this->template->content = new View('admin/messages/main');
  36. // Get Title
  37. $service = ORM::factory('service', $service_id);
  38. $this->template->content->title = $service->service_name;
  39. // Display Reply to Option?
  40. $this->template->content->reply_to = TRUE;
  41. if ( ! Kohana::config("settings.sms_provider"))
  42. {
  43. // Hide Reply to option
  44. $this->template->content->reply_to = FALSE;
  45. }
  46. // Is this an Inbox or Outbox Filter?
  47. if (!empty($_GET['type']))
  48. {
  49. $type = $_GET['type'];
  50. if ($type == '2')
  51. {
  52. // OUTBOX
  53. $filter = 'message.message_type = 2';
  54. }
  55. else
  56. {
  57. // INBOX
  58. $type = "1";
  59. $filter = 'message.message_type = 1';
  60. }
  61. }
  62. else
  63. {
  64. $type = "1";
  65. $filter = 'message.message_type = 1';
  66. }
  67. // Do we have a reporter ID?
  68. if (isset($_GET['rid']) AND !empty($_GET['rid']))
  69. {
  70. $filter .= ' AND '.$table_prefix.'message.reporter_id=\''.intval($_GET['rid']).'\'';
  71. }
  72. // ALL / Trusted / Spam
  73. $level = '0';
  74. if (isset($_GET['level']) AND !empty($_GET['level']))
  75. {
  76. $level = $_GET['level'];
  77. if ($level == 4)
  78. {
  79. $filter .= " AND ( ".$table_prefix."reporter.level_id = '4' OR "
  80. . $table_prefix."reporter.level_id = '5' ) "
  81. . "AND ( ".$table_prefix."message.message_level != '99' ) ";
  82. }
  83. elseif ($level == 2)
  84. {
  85. $filter .= " AND ( ".$table_prefix."message.message_level = '99' ) ";
  86. }
  87. }
  88. // Check, has the form been submitted?
  89. $form_error = FALSE;
  90. $form_saved = FALSE;
  91. $form_action = "";
  92. // Check, has the form been submitted, if so, setup validation
  93. if ($_POST)
  94. {
  95. // Instantiate Validation, use $post, so we don't overwrite $_POST fields with our own things
  96. $post = Validation::factory($_POST);
  97. // Add some filters
  98. $post->pre_filter('trim', TRUE);
  99. // Add some rules, the input field, followed by a list of checks, carried out in order
  100. $post->add_rules('action','required', 'alpha', 'length[1,1]');
  101. $post->add_rules('message_id.*','required','numeric');
  102. // Test to see if things passed the rule checks
  103. if ($post->validate())
  104. {
  105. if( $post->action == 'd' ) // Delete Action
  106. {
  107. foreach($post->message_id as $item)
  108. {
  109. // Delete Message
  110. $message = ORM::factory('message')->find($item);
  111. $message->message_type = 3; // Tag As Deleted/Trash
  112. $message->save();
  113. }
  114. $form_saved = TRUE;
  115. $form_action = utf8::strtoupper(Kohana::lang('ui_admin.deleted'));
  116. }
  117. elseif ($post->action == 'n')
  118. {
  119. // Not Spam
  120. foreach($post->message_id as $item)
  121. {
  122. // Update Message Level
  123. $message = ORM::factory('message')->find($item);
  124. if ($message->loaded)
  125. {
  126. $message->message_level = '1';
  127. $message->save();
  128. }
  129. }
  130. $form_saved = TRUE;
  131. $form_action = utf8::strtoupper(Kohana::lang('ui_admin.modified'));
  132. }
  133. elseif ($post->action == 's')
  134. {
  135. // Spam
  136. foreach ($post->message_id as $item)
  137. {
  138. // Update Message Level
  139. $message = ORM::factory('message')->find($item);
  140. if ($message->loaded)
  141. {
  142. $message->message_level = '99';
  143. $message->save();
  144. }
  145. }
  146. $form_saved = TRUE;
  147. $form_action = utf8::strtoupper(Kohana::lang('ui_admin.modified'));
  148. }
  149. }
  150. // No! We have validation errors, we need to show the form again, with the errors
  151. else
  152. {
  153. // repopulate the form fields
  154. $form = arr::overwrite($form, $post->as_array());
  155. // populate the error fields, if any
  156. $errors = arr::overwrite($errors, $post->errors('message'));
  157. $form_error = TRUE;
  158. }
  159. }
  160. // Pagination
  161. $pagination = new Pagination(array(
  162. 'query_string' => 'page',
  163. 'items_per_page' => $this->items_per_page,
  164. 'total_items' => ORM::factory('message')
  165. ->join('reporter','message.reporter_id','reporter.id')
  166. ->where($filter)
  167. ->where('service_id', $service_id)
  168. ->count_all()
  169. ));
  170. $messages = ORM::factory('message')
  171. ->join('reporter','message.reporter_id','reporter.id')
  172. ->where('service_id', $service_id)
  173. ->where($filter)
  174. ->orderby('message_date','desc')
  175. ->find_all($this->items_per_page, $pagination->sql_offset);
  176. // Get Message Count
  177. // ALL
  178. $this->template->content->count_all = ORM::factory('message')
  179. ->join('reporter','message.reporter_id','reporter.id')
  180. ->where('service_id', $service_id)
  181. ->where('message_type', 1)
  182. ->count_all();
  183. // Trusted
  184. $this->template->content->count_trusted = ORM::factory('message')
  185. ->join('reporter','message.reporter_id','reporter.id')
  186. ->where('service_id', $service_id)
  187. ->where('message_type', 1)
  188. ->where("message.message_level != '99' AND ( ".$table_prefix."reporter.level_id = '4' OR ".$table_prefix."reporter.level_id = '5' )")
  189. ->count_all();
  190. // Spam
  191. $this->template->content->count_spam = ORM::factory('message')
  192. ->join('reporter','message.reporter_id','reporter.id')
  193. ->where('service_id', $service_id)
  194. ->where('message_type', 1)
  195. ->where("message.message_level = '99'")
  196. ->count_all();
  197. //Reporters
  198. $this->template->content->count_reporters = ORM::factory('reporter')
  199. ->where('service_id', $service_id)
  200. ->count_all();
  201. $this->template->content->messages = $messages;
  202. $this->template->content->service_id = $service_id;
  203. $this->template->content->services = ORM::factory('service')->find_all();
  204. $this->template->content->pagination = $pagination;
  205. $this->template->content->form_error = $form_error;
  206. $this->template->content->form_saved = $form_saved;
  207. $this->template->content->form_action = $form_action;
  208. $levels = ORM::factory('level')->orderby('level_weight')->find_all();
  209. $this->template->content->levels = $levels;
  210. // Total Reports
  211. $this->template->content->total_items = $pagination->total_items;
  212. // Message Type Tab - Inbox/Outbox
  213. $this->template->content->type = $type;
  214. $this->template->content->level = $level;
  215. // Javascript Header
  216. $this->themes->js = new View('admin/messages/messages_js');
  217. }
  218. /**
  219. * Send A New Message Using Default SMS Provider
  220. */
  221. public function send()
  222. {
  223. $this->template = "";
  224. $this->auto_render = FALSE;
  225. // Setup and initialize form field names
  226. $form = array(
  227. 'to_id' => '',
  228. 'message' => ''
  229. );
  230. // Copy the form as errors, so the errors will be stored with keys
  231. // corresponding to the form field names
  232. $errors = $form;
  233. $form_error = FALSE;
  234. // Check, has the form been submitted, if so, setup validation
  235. if ($_POST)
  236. {
  237. // Instantiate Validation, use $post, so we don't overwrite $_POST
  238. // fields with our own things
  239. $post = new Validation($_POST);
  240. // Add some filters
  241. $post->pre_filter('trim', TRUE);
  242. // Add some rules, the input field, followed by a list of checks, carried out in order
  243. $post->add_rules('to_id', 'required', 'numeric');
  244. $post->add_rules('message', 'required', 'length[1,160]');
  245. // Test to see if things passed the rule checks
  246. if ($post->validate())
  247. {
  248. // Yes! everything is valid
  249. $reply_to = ORM::factory('message', $post->to_id);
  250. if ($reply_to->loaded == true)
  251. {
  252. // Yes! Replyto Exists
  253. // This is the message we're replying to
  254. $sms_to = $reply_to->message_from;
  255. //checks if the number is encrypted
  256. if (preg_match("/([a-zA-Z])(\D)/", $sms_to))
  257. {
  258. $this->decrypter = new Encrypt;
  259. $sms_to = $this->decrypter->decode($sms_to);
  260. }
  261. else
  262. {
  263. $sms_to = $sms_to;
  264. }
  265. // Load Users Settings
  266. $settings = Settings_Model::get_array();
  267. if ( !empty($settings))
  268. {
  269. // Get SMS Numbers
  270. if ( ! empty($settings['sms_no1']))
  271. {
  272. $sms_from = $settings['sms_no1'];
  273. }
  274. elseif ( ! empty($settings['sms_no2']))
  275. {
  276. $sms_from = $settings['sms_no2'];
  277. }
  278. elseif ( ! empty($settings['sms_no3']))
  279. {
  280. $sms_from = $settings['sms_no3'];
  281. }
  282. else
  283. {
  284. // User needs to set up an SMS number
  285. $sms_from = "000";
  286. }
  287. // Send Message
  288. $response = sms::send($sms_to, $sms_from, $post->message);
  289. // Message Went Through??
  290. if ($response === TRUE)
  291. {
  292. $message = ORM::factory('message');
  293. $message->parent_id = $post->to_id; // The parent message
  294. $message->message_from = $sms_from;
  295. $message->message_to = $sms_to;
  296. $message->message = $post->message;
  297. $message->message_type = 2; // This is an outgoing message
  298. $message->reporter_id = $reply_to->reporter_id;
  299. $message->message_date = date("Y-m-d H:i:s",time());
  300. $message->save();
  301. echo json_encode(array(
  302. "status" => "sent",
  303. "message" => Kohana::lang('ui_admin.message_sent')
  304. ));
  305. }
  306. else
  307. {
  308. // Message Failed
  309. echo json_encode(array(
  310. "status" => "error",
  311. "message" => Kohana::lang('ui_admin.error_msg')." - " . $response
  312. ));
  313. }
  314. }
  315. else
  316. {
  317. echo json_encode(array(
  318. "status" => "error",
  319. "message" => Kohana::lang('ui_admin.error_msg').Kohana::lang('ui_admin.check_sms_settings')
  320. ));
  321. }
  322. }
  323. else
  324. {
  325. // Send_To Mobile Number Doesn't Exist
  326. echo json_encode(array(
  327. "status" => "error",
  328. "message" => Kohana::lang('ui_admin.error_msg').Kohana::lang('ui_admin.check_number')
  329. ));
  330. }
  331. }
  332. // No! We have validation errors, we need to show the form again,
  333. // with the errors
  334. else
  335. {
  336. // Populate the error fields, if any
  337. $errors = arr::overwrite($errors, $post->errors('messages'));
  338. echo json_encode(array(
  339. "status" => "error",
  340. "message" => Kohana::lang('ui_admin.error_msg').Kohana::lang('ui_admin.check_message_valid')
  341. ));
  342. }
  343. }
  344. }
  345. /**
  346. * Setup simplepie
  347. * @param string $raw_data
  348. */
  349. private function _setup_simplepie($raw_data)
  350. {
  351. $data = new SimplePie();
  352. $data->set_raw_data( $raw_data );
  353. $data->enable_cache(false);
  354. $data->enable_order_by_date(true);
  355. $data->init();
  356. $data->handle_content_type();
  357. return $data;
  358. }
  359. }