File: sms_to_wordpress.php

package info (click to toggle)
kalkun 0.8.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,340 kB
  • sloc: php: 30,659; javascript: 30,443; sql: 961; sh: 766; xml: 105; makefile: 41
file content (96 lines) | stat: -rw-r--r-- 2,735 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
* Plugin Name: SMS to Wordpress
* Plugin URI: http://azhari.harahap.us
* Version: 0.1
* Description: Posting Wordpress blog using SMS
* Author: Azhari Harahap
* Author URI: http://azhari.harahap.us
*/

require_once (APPPATH . 'plugins/Plugin_helper.php');

class Sms_to_wordpress_plugin extends CI3_plugin_system
{
	use plugin_trait;

	public function __construct()
	{
		parent::__construct();
		// Add hook for incoming message
		add_filter('message.incoming.before', array($this, 'sms_to_wordpress'), 16);
	}

	// ------------------------------------------------------------------------

	/**
	 * Install Plugin
	 *
	 * Anything that needs to happen when this plugin gets installed
	 *
	 * @access public
	 * @since   0.1.0
	 * @return bool    TRUE by default
	 */
	public static function install($data = NULL)
	{
		$CI = &get_instance();
		$CI->load->helper('kalkun');
		// check if table already exist
		if ( ! $CI->db->table_exists('plugin_sms_to_wordpress'))
		{
			$db_driver = $CI->db->platform();
			$db_prop = get_database_property($db_driver);
			execute_sql(APPPATH . 'plugins/sms_to_wordpress/media/' . $db_prop['file'] . '_sms_to_wordpress.sql');
		}
		return TRUE;
	}

	function sms_to_wordpress($sms)
	{
		$config = Plugin_helper::get_plugin_config('sms_to_wordpress');
		$message = $sms->TextDecoded;
		$number = $sms->SenderNumber;

		list($code) = explode(' ', $message);
		$wordpress_code = $config['wordpress_code'];
		$wordpress_post = trim(str_replace($config['wordpress_code'], '', $message));
		if (strtoupper($code) === strtoupper($wordpress_code))
		{
			$CI = &get_instance();
			$CI->load->model('sms_to_wordpress/sms_to_wordpress_model', 'sms_to_wordpress_model');

			// if wp url exist
			$wp = $CI->sms_to_wordpress_model->get_wp_url_by_phone($number);
			if ( ! empty($wp))
			{
				$client = new IXR\Client\Client($wp['wp_url']);

				// Post parameter
				$post = array(
					'title' => 'Post from SMS',
					'description' => $wordpress_post,
					//'dateCreated' => (new IXR_Date(time())),
					'post_type' => 'post',
					'post_status' => 'publish',
					'mt_keywords' => 'sms,kalkun',
					'publish' => 1
				);

				$CI->load->library('encryption');
				$wp_pass = $CI->encryption->decrypt($wp['wp_password']);
				if ($wp_pass === FALSE)
				{
					log_message('error', 'sms_to_wordpress: problem during decryption.');
					show_error('sms_to_wordpress: problem during decryption.', 500, '500 Internal Server Error');
				}
				// Debug ON. Now you know what's going on.
				//$client->debug = true;
				// Execute
				$res = $client->query('metaWeblog.newPost', '', $wp['wp_username'], $wp_pass, $post);
			}
		}
	}
}