functions-forms-save.php 17.5 KB
Newer Older
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
<?php
/**
 * Copyright: 2022 (c)Franco (nextime) Lanza <franco@nexlab.it>
 * License: GNU/GPL version 3.0
 *
 * This file is part of SexHackMe Wordpress Plugin.
 *
 * SexHackMe Wordpress Plugin is free software: you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published 
 * by the Free Software Foundation, either version 3 of the License, 
 * or (at your option) any later version.
 *
 * SexHackMe Wordpress Plugin is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License 
 * along with SexHackMe Wordpress Plugin. If not, see <https://www.gnu.org/licenses/>.
 */

namespace wp_SexHackMe;

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;

27
function save_sexhack_video_forms( $post_id)
28 29 30
{

   // Verify that the nonce is set and valid.
31 32 33 34
   
   if ((!isset( $_POST['sh_video_description_nonce']) || !wp_verify_nonce( $_POST['sh_video_description_nonce'], 'video_description_nonce' )) 
      && (!isset( $_POST['sh_editvideo_nonce']) || !wp_verify_nonce( $_POST['sh_editvideo_nonce'], 'sh_editvideo')))
   {
35 36 37
      return;
   }

38 39 40
   $admin=false;
   if(isset( $_POST['sh_video_description_nonce']) && wp_verify_nonce( $_POST['sh_video_description_nonce'], 'video_description_nonce' )) $admin=true;

41 42 43 44 45
   // We need to be executed only when post_type is set...
   if(!isset($_POST['post_type'])) return;
   // ... ant it's set to sexhack_video
   if($_POST['post_type']!='sexhack_video') return;

46 47 48 49
   // ... what if we are saving the wrong post?
   if(get_post_type($post_id) != 'sexhack_video') return;


50
   // Make sure we don't get caught in any loop
51 52
   if($admin) unset($_POST['sh_video_description_nonce']);
   if(!$admin) unset($_POST['sh_editvideo_nonce']);
53

54 55 56 57 58 59 60 61 62 63 64 65 66
   // If this is an autosave, our form has not been submitted, so we don't want to do anything.
   if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
      return;
   }

   // Check the user's permissions.
   if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {

      if ( ! current_user_can( 'edit_page', $post_id ) ) {
         return;
      }

   }
67
   else { // XXX Add more specific permission for our pages?
68 69 70 71 72 73
      if ( ! current_user_can( 'edit_post', $post_id ) ) {
          return;
      }
   }
   /* OK, it's safe for us to save the data now. */

74 75 76 77

   // **** VIDEO OBJECT CREACTION **** //


78 79 80 81 82 83
   // Make sure that it is set.
   if ( ! isset( $_POST['video_description'] ) ) {
      return;
   }

   // Get $video object
84
   $setslug = false;
85
   $newvideo = true;
86
   $video = sh_get_video_from_post($post_id);
87 88 89
   if(!$video) sexhack_log("Video object not initialized, new video?? (form passed \"$post_id\" \$post_id");
   if(!$video) sexhack_log($video);
   if(!$video) $setslug = true;
90
	if(!$video) $newvideo = true;
91 92 93
   if(!$video) $video = new SH_Video();


94 95
	// **** VIDEO OBJECT BASIC SETTINGS **** //	

96 97 98 99 100 101 102 103 104 105
   // Set post_id
   $video->post_id = $post_id;

   // set post
   $post = $video->get_post();

   // XXX TODO Sanitize inputs!
   //
   // Title and slug 
   $video->title = $post->post_title;
106 107
   if(!$video->slug || $setslug) $video->slug = uniqidReal()."-".$post->post_name;

108 109 110 111 112 113 114
   // Model
   if($admin) {
   	if(array_key_exists('video_model', $_POST) && is_numeric($_POST['video_model']) && intval($_POST['video_model']) > 0)
         $video->user_id = intval($_POST['video_model']);
   } else {
      $video->user_id = get_current_user_id();
   }
115 116

   // Video description
117 118
 	//$video->description = sanitize_text_field( $_POST['video_description'] );
   $video->description = $_POST['video_description'];
119

120 121 122 123 124 125 126 127

	
   // Save video status as "creating" to get video id
	if($newvideo) 
	{
		$video->status = "creating";
		$video = sh_save_video($video, 'FORM');	
	}
128 129 130 131 132

   // Video status
   $validstatuses = array('creating','uploading','queue','processing','ready','published','error');
   if(array_key_exists('video_status', $_POST) && in_array(sanitize_text_field($_POST['video_status']), $validstatuses))
      $video->status = sanitize_text_field($_POST['video_status']);
133
   else if(!$admin)  $video->status = get_post_status($post_id);
134 135 136 137 138

   // Video private
   if(array_key_exists('video_private', $_POST) && in_array($_POST['video_private'], array('Y','N')))
      $video->private = $_POST['video_private'];

139 140 141 142 143
   // Premium Video is PPV?
   if(array_key_exists('premium_is_ppv', $_POST) && in_array($_POST['premium_is_ppv'], array('Y','N')))
      $video->premium_is_ppv = $_POST['premium_is_ppv'];


144 145
   // Video visible
   if(array_key_exists('video_visible', $_POST) && in_array($_POST['video_visible'], array('Y','N')))
146
      $video->visible = $_POST['video_visible'];
147 148

	// Video price
149 150 151
	if(array_key_exists('video_price', $_POST) && is_numeric($_POST['video_price']) && (floatval($_POST['video_price']) >= 0))
      $video->price = floatval($_POST['video_price']);
   else $video->price = 0;
152 153 154 155 156

	// Video type
	if(array_key_exists('video_type', $_POST) && in_array($_POST['video_type'], array('VR', 'FLAT')))
		$video->video_type = $_POST['video_type'];

157

158 159 160 161
	// VR Projection
	if(array_key_exists('video_vr_projection', $_POST) && in_array($_POST['video_vr_projection'], array('VR180_LR','VR360_LR')))
		$video->vr_projection = $_POST['video_vr_projection'];

162
	// **** VIDEO ASSOCIATIVE ARRAYS **** //
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
   // Video Guests
   if(array_key_exists('vguests', $_POST) && is_array($_POST['vguests']))
   {
      foreach($_POST['vguests'] as $guest_id)
      {
         if(is_numeric($guest_id) && intval($guest_id) > 0)
         {
            if($admin || (!$admin && intval($guest_id) != get_current_user_id())) {
               $guest = get_userdata(intval($guest_id));
               if($guest) $video->add_guest($guest);
            }
         }
      }
   }
   // Make sure the guestss array is initialized
   $video->get_guests(false);


   // Video Categories
   if(array_key_exists('vcategory', $_POST) && is_array($_POST['vcategory']))
   {
      foreach($_POST['vcategory'] as $cat_id)
      {
         if(is_numeric($cat_id) && intval($cat_id) > 0)
         {
            $cat = sh_get_categories(intval($cat_id));
            if($cat) $video->add_category($cat);
         }
      }
   }
   // Make sure the categories array is initialized
   $video->get_categories(false);

   // Video Tags
   if(array_key_exists('video_tags', $_POST) && is_array($_POST['video_tags']))
   {
      foreach($_POST['video_tags'] as $tag_name)
      {
         $tag_name = str_replace("#", "", $tag_name);
         $vtags = $video->get_tags(false);
         if(sanitize_text_field(strtolower($tag_name)))
         {
            $tag_name = sanitize_text_field(strtolower($tag_name));
            $tag = sh_get_tag_by_name($tag_name, true);
            if($tag) $video->add_tag($tag);
         }  
         
      }  
   }  
   // Make sure the tags array is initialized
   $video->get_tags(false);
216 217


218 219
	// **** HLS AND DOWNLOAD VIDEO PROCESSING **** //

220
	// Differenciated content for access levels
221
	$public_exists=false;
222 223
   $members_exists=false;
   $selectedv=false;
224
	foreach(array('public','members','premium') as $vt)
225
   {
226 227 228 229 230 231
      if(!$admin &&  array_key_exists('video_'.$vt, $_POST) &&
         sanitize_text_field($_POST['video_'.$vt]) && !$selectedv)
      {
         $selectedv = get_option('sexhack_video_tmp_path', '/tmp')."/".sanitize_text_field($_POST['video_'.$vt]);
      }

232
		// HLS playlist 
233
		if($admin &&array_key_exists('video_hls_'.$vt, $_POST) && 
234
			sanitize_text_field($_POST['video_hls_'.$vt]) &&
235 236 237
			(strncasecmp(strrev(sanitize_text_field($_POST['video_hls_'.$vt])), '8u3m', 4) === 0)) 
		{
			$video->__set('hls_'.$vt, sanitize_text_field($_POST['video_hls_'.$vt]));
238 239
      }
      else if(!$admin &&  array_key_exists('video_'.$vt, $_POST) && 
240
         sanitize_text_field($_POST['video_'.$vt])) 
241
      {
242 243 244 245
         //$video->__set('hls_'.$vt, get_option('sexhack_video_tmp_path', '/tmp')."/".sanitize_text_field($_POST['video_'.$vt]));
			sh_add_video_job($video->id, 'process_hls_'.$vt, get_option('sexhack_video_tmp_path', '/tmp')."/".sanitize_text_field($_POST['video_'.$vt]));
			if($vt=='public') $public_exists=true;
			if($vt=='members') 
246
         {
247 248 249 250 251 252
				$members_exists=true;
				if(array_key_exists('video_createPublic_'.$vt, $_POST) && 
					array_key_exists('video_createPublicStart_'.$vt, $_POST) &&
					\DateTime::createFromFormat('H:i:s',$_POST['video_createPublicStart_'.$vt]) &&
					array_key_exists('video_createPublicDuration_'.$vt, $_POST) &&
					is_numeric($_POST['video_createPublicDuration_'.$vt]) && intval($_POST['video_createPublicDuration_'.$vt]) > 0 &&
253
					in_array($_POST['video_createPublic_'.$vt], array('Y')) && !$public_exists)
254 255 256 257 258 259 260 261 262
				{
					$file=get_option('sexhack_video_tmp_path', '/tmp')."/".sanitize_text_field($_POST['video_'.$vt]);
					$start=$_POST['video_createPublicStart_'.$vt];
					$duration=$_POST['video_createPublicDuration_'.$vt];
					sh_add_video_job($video->id, 'create_hls_public', json_encode(array('file' => $file, 'start' => $start, 'duration' => $duration )));
					$public_exists=true;
				}
			}
			if($vt=='premium') 
263
         {
264 265 266 267 268
            if(array_key_exists('video_createPublic_'.$vt, $_POST) &&
               array_key_exists('video_createPublicStart_'.$vt, $_POST) && 
               \DateTime::createFromFormat('H:i:s',$_POST['video_createPublicStart_'.$vt]) &&
               array_key_exists('video_createPublicDuration_'.$vt, $_POST) &&
               is_numeric($_POST['video_createPublicDuration_'.$vt]) && intval($_POST['video_createPublicDuration_'.$vt]) > 0 &&
269
               in_array($_POST['video_createPublic_'.$vt], array('Y')) && !$public_exists)
270
            {
271

272 273 274 275 276 277 278 279 280 281
               $file=get_option('sexhack_video_tmp_path', '/tmp')."/".sanitize_text_field($_POST['video_'.$vt]);
               $start=$_POST['video_createPublicStart_'.$vt];
               $duration=$_POST['video_createPublicDuration_'.$vt];
               sh_add_video_job($video->id, 'create_hls_public', json_encode(array('file' => $file, 'start' => $start, 'duration' => $duration )));
               $public_exists=true;
            }  
            if(array_key_exists('video_createMembers_'.$vt, $_POST) &&
               array_key_exists('video_createMembersStart_'.$vt, $_POST) && 
               \DateTime::createFromFormat('H:i:s',$_POST['video_createMembersStart_'.$vt]) &&
               array_key_exists('video_createMembersDuration_'.$vt, $_POST) &&
282
               is_numeric($_POST['video_createMembersDuration_'.$vt]) && intval($_POST['video_createMembersDuration_'.$vt]) > 0 &&
283
               in_array($_POST['video_createMembers_'.$vt], array('Y')) && !$members_exists)
284 285 286 287 288
            {
               $file=get_option('sexhack_video_tmp_path', '/tmp')."/".sanitize_text_field($_POST['video_'.$vt]);
               $start=$_POST['video_createMembersStart_'.$vt];
               $duration=$_POST['video_createMembersDuration_'.$vt];
               sh_add_video_job($video->id, 'create_hls_members', json_encode(array('file' => $file, 'start' => $start, 'duration' => $duration )));
289
               $members_exists=true;
290 291 292 293
            }  

			}
			$video->__set('hls_'.$vt, false);
294 295
      }
      else $video->__set('hls_'.$vt, false);
296 297
	
      // Download 
298
      if($admin && array_key_exists('video_download_'.$vt, $_POST) &&
299
         sanitize_text_field($_POST['video_download_'.$vt]))
300 301
      {  
         $video->__set('download_'.$vt, sanitize_text_field($_POST['video_download_'.$vt]));
302 303
      }
      else if(!$admin && array_key_exists($vt.'_isdownload', $_POST) &&  
304
         in_array($_POST[$vt.'_isdownload'], array('Y')) && array_key_exists('video_'.$vt, $_POST) && 
305
         sanitize_text_field($_POST['video_'.$vt])) 
306 307 308 309
		{
            //$video->__set('download_'.$vt, get_option('sexhack_video_tmp_path', '/tmp')."/".sanitize_text_field($_POST['video_'.$vt]));
				sh_add_video_job($video->id, 'process_download_'.$vt, get_option('sexhack_video_tmp_path', '/tmp')."/".sanitize_text_field($_POST['video_'.$vt]));
		}
310
      else $video->__set('download_'.$vt, false);
311 312
  
		// Text only data
313 314 315 316 317 318 319 320 321
      if($admin) {
		   foreach(array('size','format','codec','acodec','duration','resolution') as $key)
		   {
      	   if(array_key_exists('video_'.$key.'_'.$vt, $_POST) &&
         	   sanitize_text_field($_POST['video_'.$key.'_'.$vt]))
      	   {  
         	   $video->__set($key.'_'.$vt, sanitize_text_field($_POST['video_'.$key.'_'.$vt]));
      	   } else $video->__set($key.'_'.$vt, false);
         }
322 323 324 325
		} 
	
	}

326 327 328 329 330 331 332 333 334
	// **** VIDEO EXTRACTIONS PROPERTIES (XXX those NEEDS to be AFTER video processing) **** //

   // Video thumbnail
   if($admin) {
      if(array_key_exists('video_thumbnail', $_POST) && sanitize_text_field($_POST['video_thumbnail']))
         $video->thumbnail = sanitize_text_field( $_POST['video_thumbnail'] );
      else if(array_key_exists('_thumbnail_id', $_POST)
         && is_numeric($_POST['_thumbnail_id'])
         && intval($_POST['_thumbnail_id']) > 0)
335
      {
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
         $video->thumbnail = intval($_POST['_thumbnail_id']);
      }
      else
         $video->thumbnail = false;
   } else {
      // Shoudn't we move it somewhere?
      if(isset($_POST['video_thumb'])
         && !empty($_POST['video_thumb'])) 
      {
         //$video->thumbnail = get_option('sexhack_video_tmp_path', '/tmp')."/".sanitize_text_field($_POST['video_thumb']);
         sh_add_video_job($video->id, 'process_thumb', get_option('sexhack_video_tmp_path', '/tmp')."/".sanitize_text_field($_POST['video_thumb'])); 
         $video->thumbnail = false;
      }
      else
      {
         if($selectedv) sh_add_video_job($video->id, 'create_thumb', $selectedv); 
         $video->thumbnail = false;
353 354 355 356
      }
   }


357
   // Animated gif path
358
   if($admin &&  array_key_exists('video_gif', $_POST) && sanitize_text_field($_POST['video_gif']))
359 360 361
      $video->gif = sanitize_text_field($_POST['video_gif']);
   elseif(!$admin &&  array_key_exists('video_gif', $_POST) &&
      sanitize_text_field($_POST['video_gif'])) 
362
   {
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
      //$video->gif = sanitize_text_field(get_option('sexhack_video_tmp_path', '/tmp')."/".$_POST['video_gif']);
		sh_add_video_job($video->id, 'process_gif', get_option('sexhack_video_tmp_path', '/tmp')."/".sanitize_text_field($_POST['video_gif']));
		$video->gif = false;
	}
	elseif(!$admin && 
		array_key_exists('video_createStart_gif', $_POST) &&
		\DateTime::createFromFormat('H:i:s',$_POST['video_createStart_gif']) && 
		array_key_exists('video_createDuration_gif', $_POST) &&
		is_numeric($_POST['video_createDuration_gif']) && intval($_POST['video_createDuration_gif']) > 0 &&
		array_key_exists('video_createFPS_gif', $_POST) &&
		is_numeric($_POST['video_createFPS_gif']) && intval($_POST['video_createFPS_gif']) > 0 )
	{
		$duration=$_POST['video_createDuration_gif'];
		$start=$_POST['video_createStart_gif'];
		$fps=$_POST['video_createFPS_gif'];
378
		sh_add_video_job($video->id, 'create_gif', json_encode(array('start' => $start, 'fps' => $fps, 'duration' => $duration, 'file' => $selectedv )));
379 380 381 382
		$video->gif = false;
	}
   else
      $video->gif = false;
383

384
   // Small Animated gif path
385
   if($admin && array_key_exists('video_gif_small', $_POST) && sanitize_text_field($_POST['video_gif_small']))
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
      $video->gif_small = sanitize_text_field($_POST['video_gif_small']);
   elseif(!$admin && array_key_exists('video_gif_small', $_POST) &&
      sanitize_text_field($_POST['video_gif_small']))
	{
      //$video->gif_small = sanitize_text_field(get_option('sexhack_video_tmp_path', '/tmp')."/".$_POST['video_gif_small']);
		sh_add_video_job($video->id, 'process_gif_small', get_option('sexhack_video_tmp_path', '/tmp')."/".sanitize_text_field($_POST['video_gif_small']));
		$video->gif_small = false;
	}
	elseif(!$admin &&
      array_key_exists('video_createStart_gif_small', $_POST) &&
      \DateTime::createFromFormat('H:i:s',$_POST['video_createStart_gif_small']) &&
      array_key_exists('video_createDuration_gif_small', $_POST) &&
      is_numeric($_POST['video_createDuration_gif_small']) && intval($_POST['video_createDuration_gif_small']) > 0 &&
      array_key_exists('video_createFPS_gif_small', $_POST) &&
      is_numeric($_POST['video_createFPS_gif_small']) && intval($_POST['video_createFPS_gif_small']) > 0 )
401
   {
402 403 404
      $duration=$_POST['video_createDuration_gif_small'];
      $start=$_POST['video_createStart_gif_small'];
      $fps=$_POST['video_createFPS_gif_small'];
405
      sh_add_video_job($video->id, 'create_gif_small', json_encode(array('start' => $start, 'fps' => $fps, 'duration' => $duration, 'file' => $selectedv )));
406 407 408 409
      $video->gif_small = false;
   }
   else
      $video->gif_small = false;
410

411
   // Preview video
412
   if($admin && array_key_exists('video_preview', $_POST) && sanitize_text_field($_POST['video_preview']))
413 414 415 416 417 418
      $video->preview = sanitize_text_field($_POST['video_preview']);
   elseif(!$admin && array_key_exists('video_preview', $_POST) &&
      sanitize_text_field($_POST['video_preview']))
   {
      //$video->preview = sanitize_text_field(get_option('sexhack_video_tmp_path', '/tmp')."/".$_POST['video_preview']);
      sh_add_video_job($video->id, 'process_preview', sanitize_text_field(get_option('sexhack_video_tmp_path', '/tmp')."/".$_POST['video_preview']));
419
   }
420 421 422
   else
      $video->preview = false;

423 424 425
   // Socialpost
   if(!$admin)
   {
426

427 428 429
      //sh_add_video_job($video->id, 'socialpost_text'); // Not needed. shmproc will give you the text to do if it doesn't exists already
      sh_add_video_job($video->id, 'socialpost_media', $selectedv);
   }
430 431


432
   // Save the video data in the database.
433 434 435
   //sexhack_log("SAVING VIDEO FROM FORM");
   //sexhack_log($video);
   sh_save_video($video, 'FORM');
436 437 438 439 440 441

}



?>