forms.php
9.83 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<?php
//namespace Tz\WordPress\Tools\HybridGallery;
error_reporting(E_ALL ^ E_DEPRECATED);
//use Tz\WordPress\Tools;
require_once("../../../../../wp-config.php");
Tz\import('Zend', '1.9');
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata_HttpClient');
Zend_Loader::loadClass('Zend_Gdata_YouTube');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
function trace($d,$die = false) {
print "<pre>";
print_r($d);
print "</pre>";
if($die) { die('-- end of trace --'); }
}
class Forms {
protected $postdata = array();
protected $form = null;
protected $post_type = 'gallery';
protected $gallery = '';
protected $db;
protected $settings;
protected $requires_approval = "no";
public function __construct($db,$user) {
$this->db = $db;
$this->user = $user;
}
public function set_form($form_type) {
$this->form = $form_type;
}
public function set_data($data = array()) {
if (count($data) > 0) {
// remove unecessary indexes.
unset($data['approval']);
unset($data['form_type']);
unset($data['gallery']);
$this->postdata = $this->_sanitize_data($data);
}
}
public function set_approval($a) {
$this->requires_approval = $a;
}
protected function getRealIpAddr() {
if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}
public function set_gallery($gallery) {
$this->gallery = $gallery;
}
public function set_post_type($post_type) {
$this->post_type = $post_type;
}
public function set_settings($setting) {
$this->settings[] = $settings;
}
protected function extractYouTubeVideoID($link = "") {
$parsed = @parse_url($link);
if($parsed) {
if(isset($parsed['query'])) {
$querystring = $parsed['query'];
@parse_str($querystring, $output);
if( isset($output['v']) && strlen($output['v']) > 1 ) {
return $output['v'];
} else {
return false;
}
} else {
return false;
}
} else {
return false;
}
}
protected function url_exists($url='') {
// Version 4.x supported
$handle = curl_init($url);
if (false === $handle) {
return false;
}
curl_setopt($handle, CURLOPT_HEADER, false);
curl_setopt($handle, CURLOPT_FAILONERROR, true); // this works
curl_setopt($handle, CURLOPT_HTTPHEADER, Array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15") ); // request as if Firefox
curl_setopt($handle, CURLOPT_NOBODY, true);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, false);
$connectable = curl_exec($handle);
curl_close($handle);
return $connectable;
}
public function create_entry() {
$gallery = $this->get_gallery($this->gallery);
$post = new hgPost();
$post->post_type = 'gallery';
if ($this->form == "image-form") {
$post->post_title = $this->postdata['title'];
$post->post_content = $this->postdata['description'];
$post->post_status = ($this->settings['image-approval'] == 'yes') ? 'pending' : 'publish';
// verified the link.
if(isset($this->postdata['imageLinked']) && $this->postdata['imageLinked'] != "") {
if (!$this->url_exists($this->postdata['imageLinked'])) {
die(json_encode(array('status'=>'error','details'=>"I'm sorry. We tried to find that image, but couldn't.")));
}
}
$status = "verified";
$meta = array(
'gallery_type' => 'image'
, 'source' => (isset($this->postdata['imageLinked']) && $this->postdata['imageLinked'] != "") ? "linked" : "uploaded"
, 'link' => (isset($this->postdata['imageLinked']) && $this->postdata['imageLinked'] != "") ? $this->postdata['imageLinked'] : $this->postdata['uploaded_image_filename']
, 'views' => 0
, 'thumbnail' => ''
, 'tags' => ''
, 'status' => $status
);
if ($meta['source']=="uploaded") {
$post->post_status = "pending";
}
//$wpdb = $this->db;
$post_id = wp_insert_post($post);
if($post_id < 1) {
die(json_encode(array('status'=>'error','details'=>"Could not create gallery post")));
}
$this->db->query("INSERT INTO `".$this->db->prefix."term_relationships` (`object_id`,`term_taxonomy_id`) VALUES ($post_id,".$gallery['term_id'].")");
$this->db->query("UPDATE `".$this->db->prefix."term_taxonomy` SET `count`=(count+1) WHERE term_id=".$gallery['term_id']." LIMIT 1");
add_post_meta($post_id,'_gallery_item_details',$meta);
die(json_encode(array('status'=>'success','details'=>"Thank you for sharing!")));
} else {
$post->post_status = ($this->settings['video-approval'] == 'yes') ? 'pending' : 'publish';
if ( isset($this->postdata['videoLinked']) && $this->postdata['videoLinked'] != "") {
$videoID = $this->extractYouTubeVideoID($this->postdata['videoLinked']);
if($videoID) {
// go out and grab the video data....
$TzYouTubeConn = new Zend_Gdata_YouTube();
$TzYouTubeConn->setMajorProtocolVersion(2);
try {
$e = $TzYouTubeConn->getVideoEntry($videoID);
$thumbnail = "http://i.ytimg.com/vi/".$videoID."/0.jpg";
$post->post_title = htmlspecialchars( mysql_real_escape_string($e->getVideoTitle()) );
$post->post_content = htmlspecialchars( mysql_real_escape_string($e->getVideoDescription() ) );
$status = "verified";
$msg = "Thank you for sharing.";
} catch (Exception $error) {
die(json_encode(array('status'=>'error','details'=>"I'm sorry. The video could not be verified with YouTube.")));
}
} else {
die(json_encode(array('status'=>'error','details'=>"That YouTube video url is invalid.")));
}
} else {
$post->post_title = $this->postdata['video_title'];
$post->post_content = $this->postdata['video_description'];
$status = "unverified";
$msg = "Thank you for sharing. We are processing your video.";
$thumbnail = "";
}
$meta = array(
'gallery_type' => 'video'
, 'source' => (isset($this->postdata['videoLinked']) && $this->postdata['videoLinked'] != "") ? "linked" : "uploaded"
, 'link' => (isset($this->postdata['videoLinked']) && $this->postdata['videoLinked'] != "") ? $this->postdata['videoLinked'] : $this->postdata['uploaded_video_filename']
, 'views' => 0
, 'thumbnail' => $thumbnail
, 'tags' => ''
, 'status' => $status
);
$post_id = wp_insert_post($post);
$this->db->query("INSERT INTO `".$this->db->prefix."term_relationships` (`object_id`,`term_taxonomy_id`) VALUES ($post_id,".$gallery['term_id'].")");
$this->db->query("UPDATE `".$this->db->prefix."term_taxonomy` SET `count`=(count+1) WHERE term_id=".$gallery['term_id']." LIMIT 1");
add_post_meta($post_id,'_gallery_item_details',$meta);
die(json_encode(array('status'=>'success','details'=>$msg)));
}
}
public function get_gallery($slug = '') {
$result = $this->db->get_row("SELECT t.term_id,t.name, tt.description FROM `".$this->db->prefix."terms` AS t, `".$this->db->prefix."term_taxonomy` as tt WHERE tt.term_id=t.term_id AND t.slug='$slug' LIMIT 1",'ARRAY_A');
return $result;
}
protected function _sanitize_data($data = array()) {
$tempdata = array();
foreach($data as $option => $value) {
$tempdata[$option] = mysql_escape_string($value);
}
return $tempdata;
}
}
class hgPost {
var $post_title = '';
var $post_content = '';
var $post_status = 'publish';
var $post_type = 'gallery'; // can be 'page' or 'post'
var $comment_status = 'open'; // open or closed for commenting
}
$current_user = null;
get_currentuserinfo();
$form = new Forms($wpdb,$current_user);
$form->set_form($_POST['form_type']);
$form->set_gallery($_POST['gallery']);
$form->set_data($_POST);
$form->set_post_type('gallery');
$form->set_approval($_POST['approval']);
$form->create_entry();
?>