Advertisement
Guest User

FFmpeg help

a guest
Jan 21st, 2015
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 35.58 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4.  
  5. /**
  6.  
  7.  * This script used for ajax file uploads from multifields forms of SJB.
  8.  
  9.  *
  10.  
  11.  * Work flow;
  12.  
  13.  *
  14.  
  15.  * 1. looks for $_FILES and gets file names from it
  16.  
  17.  * 2. checks this names with fields incoming from form
  18.  
  19.  * 3. if names of $_FILES is presents - handle file and add it to temporary storage
  20.  
  21.  * 4. when form will be really submitted (not only ajax upload), needs to check temporary storage and gets data from it
  22.  
  23.  */
  24.  
  25.  
  26.  
  27. class SJB_Miscellaneous_AjaxFileUploadHandler extends SJB_Function
  28.  
  29. {
  30.  
  31.     private $fileUniqueId  = '';
  32.  
  33.     private $errors        = array();
  34.  
  35.     private $property      = null;
  36.  
  37.     private $propertyValue = null;
  38.  
  39.    
  40.  
  41.     public function execute()
  42.  
  43.     {
  44.  
  45.         $ajaxAction = SJB_Request::getVar('ajax_action', '', 'GET');
  46.  
  47.         $formToken  = SJB_Request::getVar('form_token', '');
  48.  
  49.  
  50.  
  51.  
  52.  
  53.         // save token date in session. In some code we needs to get list of it, and clean old tokens data from
  54.  
  55.         // session.
  56.  
  57.         self::setTokenDateToSession($formToken);
  58.  
  59.  
  60.  
  61.  
  62.  
  63.         switch ($ajaxAction) {
  64.  
  65.             // UPLOAD USER PROFILE VIDEO
  66.  
  67.             case 'upload_profile_video':
  68.  
  69.             case 'upload_profile_logo':
  70.  
  71.  
  72.  
  73.                 $uploadedFieldId = SJB_Request::getVar('uploaded_field_name', '', 'GET');
  74.  
  75.                 // get field by user group return not all fields of profile.
  76.  
  77.                 // but now we use getAllFieldsInfo() to check fields
  78.  
  79.                 $userProfileFields = SJB_UserProfileFieldManager::getAllFieldsInfo();
  80.  
  81.  
  82.  
  83.                 $fieldSid = null;
  84.  
  85.                 foreach ($userProfileFields as $field) {
  86.  
  87.                     if ($field['id'] != $uploadedFieldId) {
  88.  
  89.                         continue;
  90.  
  91.                     }
  92.  
  93.                     $fieldSid = $field['sid'];
  94.  
  95.                 }
  96.  
  97.  
  98.  
  99.                 if ($fieldSid == null) {
  100.  
  101.                     echo "Wrong profile field specified";
  102.  
  103.                     exit;
  104.  
  105.                 }
  106.  
  107.  
  108.  
  109.                 $fieldInfo  = SJB_UserProfileFieldManager::getFieldInfoBySID($fieldSid);
  110.  
  111.                 $tp         = SJB_System::getTemplateProcessor();
  112.  
  113.                 $validation = $this->validationManager($fieldInfo, $tp, $uploadedFieldId);
  114.  
  115.  
  116.  
  117.                 if ($validation === true) {
  118.  
  119.                     // video file already uploaded after isValid checks
  120.  
  121.                     // but for 'Logo' - we need some actions to make save picture
  122.  
  123.                     if ($fieldInfo['type'] == 'logo') {
  124.  
  125.                         $upload_manager = new SJB_UploadPictureManager();
  126.  
  127.                         $upload_manager->setUploadedFileID($this->fileUniqueId);
  128.  
  129.                         $upload_manager->setHeight($fieldInfo['height']);
  130.  
  131.                         $upload_manager->setWidth($fieldInfo['width']);
  132.  
  133.                         $upload_manager->uploadPicture($fieldInfo['id'], $fieldInfo);
  134.  
  135.                         // and set value of file id to property
  136.  
  137.                         $this->property->setValue($this->fileUniqueId);
  138.  
  139.                         $this->propertyValue = $this->property->getValue();
  140.  
  141.                     }
  142.  
  143.  
  144.  
  145.                     // set uploaded video to temporary value
  146.  
  147.                     if ($fieldInfo['type'] == 'video' && isset($this->propertyValue['file_id'])) {
  148.  
  149.                         $uploadedID = $this->propertyValue['file_id'];
  150.  
  151.                         // rename it to unique value
  152.  
  153.                         SJB_DB::query("UPDATE `uploaded_files` SET `id` = ?s WHERE `id` = ?s", $this->fileUniqueId, $uploadedID);
  154.  
  155.  
  156.  
  157.                         // fill session data for tmp storage
  158.  
  159.                         $fieldValue = array(
  160.  
  161.                             'file_id'         => $this->fileUniqueId,
  162.  
  163.                             'file_url'        => $this->propertyValue['file_url'],
  164.  
  165.                             'file_name'       => $this->propertyValue['file_name'],
  166.  
  167.                             'saved_file_name' => $this->propertyValue['saved_file_name'],
  168.  
  169.                         );
  170.  
  171.                         $tmpUploadsStorage = SJB_Session::getValue('tmp_uploads_storage');
  172.  
  173.                         $tmpUploadsStorage = SJB_Array::setPathValue($tmpUploadsStorage, "{$formToken}/{$uploadedFieldId}", $fieldValue);
  174.  
  175.                         SJB_Session::setValue('tmp_uploads_storage', $tmpUploadsStorage);
  176.  
  177.  
  178.  
  179.                     } elseif ($fieldInfo['type'] == 'logo') {
  180.  
  181.                         // for Logo - we already have file_url data and file_thumb data, without file_id
  182.  
  183.                         // just add this to session storage
  184.  
  185.  
  186.  
  187.                         // fill session data for tmp storage
  188.  
  189.                         $fieldValue = array(
  190.  
  191.                             'file_id'         => $this->fileUniqueId,
  192.  
  193.                             'file_url'        => $this->propertyValue['file_url'],
  194.  
  195.                             'file_name'       => $this->propertyValue['file_name'],
  196.  
  197.                             'thumb_file_url'  => $this->propertyValue['thumb_file_url'],
  198.  
  199.                             'thumb_file_name' => $this->propertyValue['thumb_file_name'],
  200.  
  201.                         );
  202.  
  203.                         $tmpUploadsStorage = SJB_Session::getValue('tmp_uploads_storage');
  204.  
  205.                         $tmpUploadsStorage = SJB_Array::setPathValue($tmpUploadsStorage, "{$formToken}/{$uploadedFieldId}", $fieldValue);
  206.  
  207.                         SJB_Session::setValue('tmp_uploads_storage', $tmpUploadsStorage);
  208.  
  209.                     }
  210.  
  211.  
  212.  
  213.                     $tp->assign(array(
  214.  
  215.                         'id'    => $uploadedFieldId,
  216.  
  217.                         'value' => $fieldValue,
  218.  
  219.                     ));
  220.  
  221.                 }
  222.  
  223.  
  224.  
  225.                 $template = '';
  226.  
  227.                 switch ($fieldInfo['type']) {
  228.  
  229.                     case 'video':
  230.  
  231.                         $template = '../field_types/input/video_profile.tpl';
  232.  
  233.                         break;
  234.  
  235.                     case 'logo':
  236.  
  237.                         $template = '../field_types/input/logo.tpl';
  238.  
  239.                         break;
  240.  
  241.                     default:
  242.  
  243.                         break;
  244.  
  245.                 }
  246.  
  247.  
  248.  
  249.                 $tp->assign('form_token', $formToken);
  250.  
  251.                 $tp->assign('errors', $this->errors);
  252.  
  253.                 $tp->display($template);
  254.  
  255.                 break;
  256.  
  257.  
  258.  
  259.             ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  260.  
  261.             case 'delete_profile_video':
  262.  
  263.             case 'delete_profile_logo':
  264.  
  265.  
  266.  
  267.                 $userSid  = SJB_Request::getVar('user_sid', null);
  268.  
  269.                 if (empty($userSid)) {
  270.  
  271.                     $userInfo = SJB_UserManager::getCurrentUserInfo();
  272.  
  273.                 } else {
  274.  
  275.                     $userInfo = SJB_UserManager::getUserInfoBySID($userSid);
  276.  
  277.                 }
  278.  
  279.  
  280.  
  281.  
  282.  
  283.                 $fieldId  = SJB_Request::getVar('field_id', null);
  284.  
  285.  
  286.  
  287.                 // check session value
  288.  
  289.                 $sessionFileStorage = SJB_Session::getValue('tmp_uploads_storage');
  290.  
  291.                 $sessionFileId      = SJB_Array::getPath($sessionFileStorage, "{$formToken}/{$fieldId}/file_id");
  292.  
  293.  
  294.  
  295.                 if (is_null($fieldId)) {
  296.  
  297.                     $this->errors['PARAMETERS_MISSED'] = 1;
  298.  
  299.                 } elseif ( (!empty($userInfo) && !isset($userInfo[$fieldId])) && empty($sessionFileId) ) {
  300.  
  301.                     echo  json_encode( array('result' => 'success') );
  302.  
  303.                     exit;
  304.  
  305.                 } else {
  306.  
  307.  
  308.  
  309.                     if (!empty($userInfo)) {
  310.  
  311.                         $uploaded_file_id = $userInfo[$fieldId];
  312.  
  313.                         SJB_UploadFileManager::deleteUploadedFileByID($uploaded_file_id);
  314.  
  315.                     }
  316.  
  317.  
  318.  
  319.  
  320.  
  321.                     if (!empty($sessionFileId)) {
  322.  
  323.                         $formFileId    = SJB_Request::getVar('file_id');
  324.  
  325.                         if ($sessionFileId == $formFileId) {
  326.  
  327.                             SJB_UploadFileManager::deleteUploadedFileByID($formFileId);
  328.  
  329.  
  330.  
  331.                             $sessionFileStorage = SJB_Array::unsetValueByPath($sessionFileStorage, "{$formToken}/{$fieldId}");
  332.  
  333.                             SJB_Session::setValue('tmp_uploads_storage', $sessionFileStorage);
  334.  
  335.                         }
  336.  
  337.                     }
  338.  
  339.                 }
  340.  
  341.  
  342.  
  343.                 if (empty($this->errors)) {
  344.  
  345.                     echo  json_encode( array('result' => 'success') ) ;
  346.  
  347.                 } else {
  348.  
  349.                     echo  json_encode( array('result' => 'error', 'errors' => $this->errors) ) ;
  350.  
  351.                 }
  352.  
  353.  
  354.  
  355.                 exit;
  356.  
  357.                 break;
  358.  
  359.  
  360.  
  361.             ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  362.  
  363.             // UPLOAD LISTIG FILES
  364.  
  365.             case 'upload_classifieds_video':
  366.  
  367.             case 'upload_file':
  368.  
  369.  
  370.  
  371.                 $uploadedFieldId = SJB_Request::getVar('uploaded_field_name', '', 'GET');
  372.  
  373.  
  374.  
  375.                 // OK. For listings form we have 'listing_id' and optional field (for new listings with temporary id) - listing_type_id
  376.  
  377.                 $listingId     = SJB_Request::getVar('listing_id');
  378.  
  379.                 $listingTypeId = SJB_Request::getVar('listing_type_id');
  380.  
  381.  
  382.  
  383.                 if (empty($listingTypeId)) {
  384.  
  385.                     $listingInfo   = SJB_ListingManager::getListingInfoBySID($listingId);
  386.  
  387.                     $listingTypeId = SJB_ListingTypeManager::getListingTypeIDBySID($listingInfo['listing_type_sid']);
  388.  
  389.                 }
  390.  
  391.                 $listingTypeSid = SJB_ListingTypeManager::getListingTypeSIDByID($listingTypeId);
  392.  
  393.  
  394.  
  395.                 $commonListingFields = SJB_ListingFieldManager::getCommonListingFieldsInfo();
  396.  
  397.                 $listingFieldsByType = SJB_ListingFieldManager::getListingFieldsInfoByListingType($listingTypeSid);
  398.  
  399.                 $listingFields       = array_merge($commonListingFields, $listingFieldsByType);
  400.  
  401.  
  402.  
  403.  
  404.  
  405.                 $fieldSid = null;
  406.  
  407.                 foreach ($listingFields as $field) {
  408.  
  409.                     if ($field['id'] != $uploadedFieldId) {
  410.  
  411.                         continue;
  412.  
  413.                     }
  414.  
  415.                     $fieldSid = $field['sid'];
  416.  
  417.                 }
  418.  
  419.  
  420.  
  421.                 $fieldInfo  = SJB_ListingFieldManager::getFieldInfoBySID($fieldSid);
  422.  
  423.                 $tp         = SJB_System::getTemplateProcessor();
  424.  
  425.                 $validation = $this->validationManager($fieldInfo, $tp, $uploadedFieldId);
  426.  
  427.  
  428.  
  429.                 if (!$validation) {
  430.  
  431.                     $tp->assign(array(
  432.  
  433.                         // and fix to listing_id param
  434.  
  435.                         'listing_id' => $listingId,
  436.  
  437.                         'listing' => array(
  438.  
  439.                             'id' => $listingId,
  440.  
  441.                         ),
  442.  
  443.                     ));
  444.  
  445.                 } else {
  446.  
  447.                     // video file already uploaded after isValid checks
  448.  
  449.                     // but for 'Logo' - we need some actions to make save picture
  450.  
  451.                     if ($this->property->getType() == 'file') {
  452.  
  453.                         if ($_FILES[$uploadedFieldId]['error']) {
  454.  
  455.                             $this->errors[SJB_UploadFileManager::getErrorId($_FILES[$uploadedFieldId]['error'])] = 1;
  456.  
  457.                         }
  458.  
  459.  
  460.  
  461.                         $upload_manager = new SJB_UploadFileManager();
  462.  
  463.                         $upload_manager->setUploadedFileID($this->fileUniqueId);
  464.  
  465.                         $upload_manager->setFileGroup('files');
  466.  
  467.                         $upload_manager->uploadFile($fieldInfo['id']);
  468.  
  469.                         // and set value of file id to property
  470.  
  471.                         $this->property->setValue($this->fileUniqueId);
  472.  
  473.                     }
  474.  
  475.  
  476.  
  477.                     $this->propertyValue = $this->property->getValue();
  478.  
  479.  
  480.  
  481.                     // set uploaded video to temporary value
  482.  
  483.                     if (isset($this->propertyValue['file_id'])) {
  484.  
  485.                         $uploadedID = $this->propertyValue['file_id'];
  486.  
  487.                         // rename it to unique value
  488.  
  489.                         SJB_DB::query("UPDATE `uploaded_files` SET `id` = ?s WHERE `id` = ?s", $this->fileUniqueId, $uploadedID);
  490.  
  491.  
  492.  
  493.  
  494.  
  495.                         // SET VALUE TO TEMPORARY SESSION STORAGE
  496.  
  497.                         $tmpUploadsStorage = SJB_Session::getValue('tmp_uploads_storage');
  498.  
  499.                         $fileValue = array(
  500.  
  501.                             'file_id'    => $this->fileUniqueId,
  502.  
  503.                             'saved_name' => $this->propertyValue['saved_file_name'],
  504.  
  505.                         );
  506.  
  507.                         $tmpUploadsStorage = SJB_Array::setPathValue($tmpUploadsStorage, "{$formToken}/{$uploadedFieldId}", $fileValue);
  508.  
  509.                         SJB_Session::setValue('tmp_uploads_storage', $tmpUploadsStorage);
  510.  
  511.  
  512.  
  513.                         // update listing property
  514.  
  515.                         $listingInfo = SJB_ListingManager::getListingInfoBySID($listingId);
  516.  
  517.                         $listing = isset($listingInfo['listing_type_sid']) ? new SJB_Listing($listingInfo, $listingInfo['listing_type_sid']) : new SJB_Listing($listingInfo);
  518.  
  519.                         $listingProperties = $listing->getProperties();
  520.  
  521.                         $propertyInfo = array(
  522.  
  523.                             'id'        => $uploadedFieldId,
  524.  
  525.                             'type'      => 'string',
  526.  
  527.                             'value'     => $this->fileUniqueId,
  528.  
  529.                             'is_system' => true,
  530.  
  531.                         );
  532.  
  533.                         foreach ($listingProperties as $property) {
  534.  
  535.                             if ($property->getID() == $uploadedFieldId) {
  536.  
  537.                                 $listing->addProperty($propertyInfo);
  538.  
  539.                             }
  540.  
  541.                         }
  542.  
  543.                         $listing->setSID($listingId);
  544.  
  545.                         SJB_ListingManager::saveListing($listing);
  546.  
  547.  
  548.  
  549.                         $tp->assign(array(
  550.  
  551.                             'id'    => $uploadedFieldId,
  552.  
  553.                             'value' => array(
  554.  
  555.                                 'file_url'        => $this->propertyValue['file_url'],
  556.  
  557.                                 'file_name'       => $this->propertyValue['file_name'],
  558.  
  559.                                 'saved_file_name' => $this->propertyValue['saved_file_name'],
  560.  
  561.                                 'file_id'         => $this->fileUniqueId,
  562.  
  563.                             ),
  564.  
  565.                             // and fix to listing_id param
  566.  
  567.                             'listing_id' => $listingId,
  568.  
  569.                             'listing' => array(
  570.  
  571.                                 'id' => $listingId,
  572.  
  573.                             ),
  574.  
  575.                         ));
  576.  
  577.                     }
  578.  
  579.                 }
  580.  
  581.  
  582.  
  583.                 switch ($this->property->getType()) {
  584.  
  585.                     case 'video':
  586.  
  587.                         $template = '../field_types/input/video.tpl';
  588.  
  589.                         break;
  590.  
  591.                     case 'file':
  592.  
  593.                         $template = '../field_types/input/file.tpl';
  594.  
  595.                         break;
  596.  
  597.                     default:
  598.  
  599.                         $template = '../field_types/input/video.tpl';
  600.  
  601.                         break;
  602.  
  603.                 }
  604.  
  605.  
  606.  
  607.                 $tp->assign('errors', $this->errors);
  608.  
  609.                 $tp->assign('form_token', $formToken);
  610.  
  611.                 $tp->display($template);
  612.  
  613.                 self::cleanOldTokensFromSession();
  614.  
  615.                 break;
  616.  
  617.  
  618.  
  619.  
  620.  
  621.             ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  622.  
  623.             case 'delete_classifieds_video':
  624.  
  625.             case 'delete_file':
  626.  
  627.  
  628.  
  629.                 $listingId  = SJB_Request::getVar('listing_id', null);
  630.  
  631.                 $fieldId    = SJB_Request::getVar('field_id', null);
  632.  
  633.                 $formFileId = SJB_Request::getVar('file_id');
  634.  
  635.                 $this->errors     = array();
  636.  
  637.  
  638.  
  639.                 // check session value
  640.  
  641.                 $sessionFileStorage = SJB_Session::getValue('tmp_uploads_storage');
  642.  
  643.                 $sessionFileId = SJB_Array::getPath($sessionFileStorage, "{$formToken}/{$fieldId}/file_id");
  644.  
  645.  
  646.  
  647.                 // if empty listing id - check end empty temporary storage
  648.  
  649.                 if (strlen($listingId) == strlen( time() )) {
  650.  
  651.                     if ($sessionFileId == $formFileId) {
  652.  
  653.                         SJB_UploadFileManager::deleteUploadedFileByID($formFileId);
  654.  
  655.                         // remove field from temporary storage
  656.  
  657.                         if (!is_null($sessionFileStorage)) {
  658.  
  659.                             $sessionFileStorage = SJB_Array::unsetValueByPath($sessionFileStorage, "{$formToken}/{$fieldId}");
  660.  
  661.                             SJB_Session::setValue('tmp_uploads_storage', $sessionFileStorage);
  662.  
  663.                         }
  664.  
  665.                     }
  666.  
  667.                 } else {
  668.  
  669.                     // we change existing listing
  670.  
  671.                     $listingInfo = SJB_ListingManager::getListingInfoBySID($listingId);
  672.  
  673.  
  674.  
  675.                     if ( (is_null($listingInfo) || !isset($listingInfo[$fieldId])) && empty($sessionFileId)) {
  676.  
  677.                         $this->errors['WRONG_PARAMETERS_SPECIFIED'] = 1;
  678.  
  679.                     }
  680.  
  681.                     else {
  682.  
  683.                         if (!$this->isOwner($listingId)) {
  684.  
  685.                             $this->errors['NOT_OWNER'] = 1;
  686.  
  687.                         }
  688.  
  689.                         else {
  690.  
  691.                             $uploadedFileId = $listingInfo[$fieldId];
  692.  
  693.                             if (!empty($uploadedFileId)) {
  694.  
  695.                                 SJB_UploadFileManager::deleteUploadedFileByID($uploadedFileId);
  696.  
  697.                             }
  698.  
  699.                             SJB_UploadFileManager::deleteUploadedFileByID($formFileId);
  700.  
  701.  
  702.  
  703.                             $listingInfo[$fieldId] = '';
  704.  
  705.                             $listing = isset($listingInfo['listing_type_sid'])? new SJB_Listing($listingInfo, $listingInfo['listing_type_sid']): new SJB_Listing($listingInfo);
  706.  
  707.                             // remove all non-changed properties and save only changed property in listing
  708.  
  709.                             $props = $listing->getProperties();
  710.  
  711.                             foreach ($props as $prop) {
  712.  
  713.                                 if ($prop->getID() !== $fieldId) {
  714.  
  715.                                     $listing->deleteProperty($prop->getID());
  716.  
  717.                                 }
  718.  
  719.                             }
  720.  
  721.                             $listing->setSID($listingId);
  722.  
  723.                             SJB_ListingManager::saveListing($listing);
  724.  
  725.  
  726.  
  727.  
  728.  
  729.                             // remove field from temporary storage
  730.  
  731.                             $sessionFileStorage = SJB_Session::getValue('tmp_uploads_storage');
  732.  
  733.                             if (!is_null($sessionFileStorage)) {
  734.  
  735.                                 $sessionFileStorage = SJB_Array::unsetValueByPath($sessionFileStorage, "{$formToken}/{$fieldId}");
  736.  
  737.                                 SJB_Session::setValue('tmp_uploads_storage', $sessionFileStorage);
  738.  
  739.                             }
  740.  
  741.                         }
  742.  
  743.                     }
  744.  
  745.                 }
  746.  
  747.  
  748.  
  749.                 if (empty($this->errors)) {
  750.  
  751.                     echo  json_encode( array('result' => 'success') ) ;
  752.  
  753.                 } else {
  754.  
  755.                     echo  json_encode( array('result' => 'error', 'errors' => $this->errors) ) ;
  756.  
  757.                 }
  758.  
  759.                 exit;
  760.  
  761.                 break;
  762.  
  763.  
  764.  
  765.  
  766.  
  767.             ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  768.  
  769.             case 'get_classifieds_video_data':
  770.  
  771.             case 'get_file_field_data':
  772.  
  773.  
  774.  
  775.                 $fieldId   = isset($_REQUEST['field_id']) ? $_REQUEST['field_id'] : null;
  776.  
  777.                 $listingId = SJB_Request::getVar('listing_id');
  778.  
  779.  
  780.  
  781.                 $filesFromTmpStorage = SJB_Session::getValue('tmp_uploads_storage');
  782.  
  783.                 $fileUniqueId        = SJB_Array::getPath($filesFromTmpStorage, "{$formToken}/{$fieldId}/file_id");
  784.  
  785.  
  786.  
  787.                 // if no temporary files uploaded, return empty string
  788.  
  789.                 if (empty($fileUniqueId)) {
  790.  
  791.                     return '';
  792.  
  793.                 }
  794.  
  795.  
  796.  
  797.                 $tp = SJB_System::getTemplateProcessor();
  798.  
  799.                 $upload_manager = new SJB_UploadFileManager();
  800.  
  801.  
  802.  
  803.                 $fileInfo = array(
  804.  
  805.                     'id'    => $fieldId,
  806.  
  807.                     'value' => array(
  808.  
  809.                         'file_url'        => $upload_manager->getUploadedFileLink($fileUniqueId),
  810.  
  811.                         'file_name'       => $upload_manager->getUploadedFileName($fileUniqueId),
  812.  
  813.                         'saved_file_name' => $upload_manager->getUploadedSavedFileName($fileUniqueId),
  814.  
  815.                         'file_id'         => $fileUniqueId,
  816.  
  817.                     ),
  818.  
  819.                     // and fix to listing_id param
  820.  
  821.                     'listing_id' => $listingId,
  822.  
  823.                     'listing' => array(
  824.  
  825.                         'id' => $listingId,
  826.  
  827.                     ),
  828.  
  829.                 );
  830.  
  831.  
  832.  
  833.                 $tp->assign($fileInfo);
  834.  
  835.  
  836.  
  837.  
  838.  
  839.                 $fieldInfo = SJB_ListingFieldDBManager::getListingFieldInfoByID($fieldId);
  840.  
  841.                 $fieldType = $fieldInfo['type'];
  842.  
  843.  
  844.  
  845.                 $template = '';
  846.  
  847.                 switch ($fieldType) {
  848.  
  849.                     case 'video':
  850.  
  851.                         $template = '../field_types/input/video.tpl';
  852.  
  853.                         break;
  854.  
  855.                     case 'file':
  856.  
  857.                         $template = '../field_types/input/file.tpl';
  858.  
  859.                         break;
  860.  
  861.                     case 'logo':
  862.  
  863.                         $template = '../field_types/input/logo_listing.tpl';
  864.  
  865.                         break;
  866.  
  867.                     default:
  868.  
  869.                         break;
  870.  
  871.                 }
  872.  
  873.  
  874.  
  875.                 $uploadedFilesize = $upload_manager->getUploadedFileSize($fileUniqueId);
  876.  
  877.                 $filesizeInfo = SJB_HelperFunctions::getFileSizeAndSizeToken($uploadedFilesize);
  878.  
  879.                 $tp->assign(array(
  880.  
  881.                         'filesize'   => $filesizeInfo['filesize'],
  882.  
  883.                         'size_token' => $filesizeInfo['size_token']
  884.  
  885.                     )
  886.  
  887.                 );
  888.  
  889.  
  890.  
  891.                 $tp->assign("uploadMaxFilesize", SJB_UploadFileManager::getIniUploadMaxFilesize());
  892.  
  893.                 $tp->assign('form_token', $formToken);
  894.  
  895.                 $tp->display($template);
  896.  
  897.                 break;
  898.  
  899.  
  900.  
  901.  
  902.  
  903.             ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  904.  
  905.             case 'upload_file_complex':
  906.  
  907.             case 'upload_classifieds_video_complex':
  908.  
  909.                 $uploadedFieldId = SJB_Request::getVar('uploaded_field_name', '', 'GET');
  910.  
  911.  
  912.  
  913.                 list($parentField, $subFieldId, $complexStep) = explode(':', $uploadedFieldId);
  914.  
  915.  
  916.  
  917.                 // OK. For listings form we have 'listing_id' and optional field (for new listings with temporary id) - listing_type_id
  918.  
  919.                 $listingId     = SJB_Request::getVar('listing_id');
  920.  
  921.                 $listingTypeId = SJB_Request::getVar('listing_type_id');
  922.  
  923.  
  924.  
  925.                 if (empty($listingTypeId)) {
  926.  
  927.                     $listingInfo   = SJB_ListingManager::getListingInfoBySID($listingId);
  928.  
  929.                     $listingTypeId = SJB_ListingTypeManager::getListingTypeIDBySID($listingInfo['listing_type_sid']);
  930.  
  931.                 }
  932.  
  933.                 $listingTypeSid = SJB_ListingTypeManager::getListingTypeSIDByID($listingTypeId);
  934.  
  935.  
  936.  
  937.                 $commonListingFields = SJB_ListingFieldManager::getCommonListingFieldsInfo();
  938.  
  939.                 $listingFieldsByType = SJB_ListingFieldManager::getListingFieldsInfoByListingType($listingTypeSid);
  940.  
  941.                 $listingFields       = array_merge($commonListingFields, $listingFieldsByType);
  942.  
  943.  
  944.  
  945.                 // check parent field
  946.  
  947.                 $fieldSid = null;
  948.  
  949.                 foreach ($listingFields as $field) {
  950.  
  951.                     if ($field['id'] != $parentField) {
  952.  
  953.                         continue;
  954.  
  955.                     }
  956.  
  957.                     $fieldSid = $field['sid'];
  958.  
  959.                 }
  960.  
  961.  
  962.  
  963.  
  964.  
  965.                 $complexFieldInfo = SJB_ListingFieldManager::getFieldInfoBySID($fieldSid);
  966.  
  967.                 $subFields = SJB_Array::get($complexFieldInfo, 'fields');
  968.  
  969.                 if (empty($subFields)) {
  970.  
  971.                     echo 'wrong field ID';
  972.  
  973.                     exit;
  974.  
  975.                 }
  976.  
  977.  
  978.  
  979.                 // check field
  980.  
  981.                 $fieldInfo = '';
  982.  
  983.                 foreach ($subFields as $subField) {
  984.  
  985.                     if ($subField['id'] != $subFieldId) {
  986.  
  987.                         continue;
  988.  
  989.                     }
  990.  
  991.                     $fieldInfo = $subField;
  992.  
  993.                 }
  994.  
  995.  
  996.  
  997.                 $complexParameters = array(
  998.  
  999.                     'parentField' => $parentField,
  1000.  
  1001.                     'subFieldId'  => $subFieldId,
  1002.  
  1003.                     'complexStep' => $complexStep
  1004.  
  1005.                 );
  1006.  
  1007.                 $tp         = SJB_System::getTemplateProcessor();
  1008.  
  1009.                 $validation = $this->validationManager($fieldInfo, $tp, $uploadedFieldId, $complexParameters);
  1010.  
  1011.  
  1012.  
  1013.                 $upload_manager = new SJB_UploadFileManager();
  1014.  
  1015.                 $upload_manager->setUploadedFileID($this->fileUniqueId);
  1016.  
  1017.                 $upload_manager->setFileGroup('files');
  1018.  
  1019.                 $upload_manager->uploadFile($fieldInfo['id'], $parentField);
  1020.  
  1021.                 $this->property->setValue($this->fileUniqueId);
  1022.  
  1023.                 $this->propertyValue = $this->property->getPropertyVariablesToAssign();
  1024.  
  1025.  
  1026.  
  1027.                 // set uploaded video to temporary value
  1028.  
  1029.                 if ((isset($this->propertyValue['value']['file_id']) || isset($this->propertyValue['value'][$complexStep]['file_id'])) && $validation) {
  1030.  
  1031.  
  1032.  
  1033.                     // fix for FILE type in complex field
  1034.  
  1035.                     if (isset($this->propertyValue['value'][$complexStep]['file_id'])) {
  1036.  
  1037.                         $this->propertyValue['value'] = $this->propertyValue['value'][$complexStep];
  1038.  
  1039.                     }
  1040.  
  1041.  
  1042.  
  1043.                     $filesInfo  = array($complexStep => $this->propertyValue['value']);
  1044.  
  1045.                     $uploadedID = $this->propertyValue['value']['file_id'];
  1046.  
  1047.  
  1048.  
  1049.                     // rename it to unique value
  1050.  
  1051.                     SJB_DB::query("UPDATE `uploaded_files` SET `id` = ?s WHERE `id` = ?s", $this->fileUniqueId, $uploadedID);
  1052.  
  1053.  
  1054.  
  1055.                     // SET VALUE TO TEMPORARY SESSION STORAGE
  1056.  
  1057.                     $tmpUploadsStorage = SJB_Session::getValue('tmp_uploads_storage');
  1058.  
  1059.                     $fileValue = array(
  1060.  
  1061.                         'file_id'    => $this->fileUniqueId,
  1062.  
  1063.                         'saved_name' => $this->propertyValue['value']['saved_file_name'],
  1064.  
  1065.                     );
  1066.  
  1067.                     $tmpUploadsStorage = SJB_Array::setPathValue($tmpUploadsStorage, "{$formToken}/{$uploadedFieldId}", $fileValue);
  1068.  
  1069.                     SJB_Session::setValue('tmp_uploads_storage', $tmpUploadsStorage);
  1070.  
  1071.  
  1072.  
  1073.                     $tp->assign(array(
  1074.  
  1075.                         'id'           => $subFieldId,
  1076.  
  1077.                         'value'        => $this->propertyValue['value']['file_name'],
  1078.  
  1079.                         'filesInfo'    => $filesInfo,
  1080.  
  1081.  
  1082.  
  1083.                         'complexField' => $parentField,
  1084.  
  1085.                         'complexStep'  => $complexStep,
  1086.  
  1087.                         // and fix to listing_id param
  1088.  
  1089.                         'listing_id' => $listingId,
  1090.  
  1091.                         'listing' => array(
  1092.  
  1093.                             'id' => $listingId,
  1094.  
  1095.                         ),
  1096.  
  1097.                     ));
  1098.  
  1099.                 }
  1100.  
  1101.                 else {
  1102.  
  1103.                     $tp->assign(array(
  1104.  
  1105.                         'id'           => $subFieldId,
  1106.  
  1107.                         'complexField' => $parentField,
  1108.  
  1109.                         'complexStep'  => $complexStep,
  1110.  
  1111.                         // and fix to listing_id param
  1112.  
  1113.                         'listing_id' => $listingId,
  1114.  
  1115.                         'listing' => array(
  1116.  
  1117.                             'id' => $listingId,
  1118.  
  1119.                         ),
  1120.  
  1121.                     ));
  1122.  
  1123.                 }
  1124.  
  1125.  
  1126.  
  1127.  
  1128.  
  1129.                 switch ($this->property->getType()) {
  1130.  
  1131.                     case 'video':
  1132.  
  1133.                         $template = '../field_types/input/video.tpl';
  1134.  
  1135.                         break;
  1136.  
  1137.                     case 'file':
  1138.  
  1139.                     case 'complexfile':
  1140.  
  1141.                         $template = '../field_types/input/file.tpl';
  1142.  
  1143.                         break;
  1144.  
  1145.  
  1146.  
  1147.                     default:
  1148.  
  1149.                         $template = '../field_types/input/video.tpl';
  1150.  
  1151.                         break;
  1152.  
  1153.                 }
  1154.  
  1155.  
  1156.  
  1157.                 $tp->assign('form_token', $formToken);
  1158.  
  1159.                 $tp->assign('errors', $this->errors);
  1160.  
  1161.                 $tp->display($template);
  1162.  
  1163.                 break;
  1164.  
  1165.  
  1166.  
  1167.  
  1168.  
  1169.             ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1170.  
  1171.             case 'delete_file_complex':
  1172.  
  1173.  
  1174.  
  1175.                 $listingId  = SJB_Request::getVar('listing_id', null);
  1176.  
  1177.                 $fieldId    = SJB_Request::getVar('field_id', null);
  1178.  
  1179.                 $formFileId = SJB_Request::getVar('file_id');
  1180.  
  1181.                 $this->errors     = array();
  1182.  
  1183.  
  1184.  
  1185.                 // check session value
  1186.  
  1187.                 $sessionFileStorage = SJB_Session::getValue('tmp_uploads_storage');
  1188.  
  1189.                 $sessionFileId      = SJB_Array::getPath($sessionFileStorage, "{$formToken}/{$fieldId}/file_id");
  1190.  
  1191.  
  1192.  
  1193.                 // if empty listing id - check and empty temporary storage
  1194.  
  1195.                 if (strlen($listingId) == strlen( time() )) {
  1196.  
  1197.                     if ($sessionFileId == $formFileId) {
  1198.  
  1199.                         SJB_UploadFileManager::deleteUploadedFileByID($formFileId);
  1200.  
  1201.                         // remove field from temporary storage
  1202.  
  1203.                         $sessionFileStorage = SJB_Array::unsetValueByPath($sessionFileStorage, "{$formToken}/{$fieldId}");
  1204.  
  1205.                         SJB_Session::setValue('tmp_uploads_storage', $sessionFileStorage);
  1206.  
  1207.                     }
  1208.  
  1209.                 } else {
  1210.  
  1211.                     // we change existing listing
  1212.  
  1213.                     $listingInfo = SJB_ListingManager::getListingInfoBySID($listingId);
  1214.  
  1215.  
  1216.  
  1217.                     list($complexField, $subField, $complexStep) = explode(':', $fieldId);
  1218.  
  1219.                     $fieldValue = SJB_Array::getPath($listingInfo, "{$complexField}/{$subField}/{$complexStep}");
  1220.  
  1221.  
  1222.  
  1223.                     // if field value not present in listing and not present in temporary storage - throw error
  1224.  
  1225.                     if ( (is_null($listingInfo) || $fieldValue === null) && empty($sessionFileId)) {
  1226.  
  1227.                         $this->errors['WRONG_PARAMETERS_SPECIFIED'] = 1;
  1228.  
  1229.                     }
  1230.  
  1231.                     else {
  1232.  
  1233.                         if (!$this->isOwner($listingId)) {
  1234.  
  1235.                             $this->errors['NOT_OWNER'] = 1;
  1236.  
  1237.                         }
  1238.  
  1239.                         else {
  1240.  
  1241.                             $uploadedFileId = $fieldValue;
  1242.  
  1243.  
  1244.  
  1245.                             if (!empty($uploadedFileId)) {
  1246.  
  1247.                                 SJB_UploadFileManager::deleteUploadedFileByID($uploadedFileId);
  1248.  
  1249.                             }
  1250.  
  1251.                             SJB_UploadFileManager::deleteUploadedFileByID($formFileId);
  1252.  
  1253.  
  1254.  
  1255.                             $listingInfo = SJB_Array::setPathValue($listingInfo, "{$complexField}/{$subField}/{$complexStep}", '');
  1256.  
  1257.  
  1258.  
  1259.                             $listing = new SJB_Listing($listingInfo, $listingInfo['listing_type_sid']);
  1260.  
  1261.                             // remove all non-changed properties and save only changed property in listing
  1262.  
  1263.                             $props = $listing->getProperties();
  1264.  
  1265.                             foreach ($props as $prop) {
  1266.  
  1267.                                 if ($prop->getID() !== $fieldId) {
  1268.  
  1269.                                     $listing->deleteProperty($prop->getID());
  1270.  
  1271.                                 }
  1272.  
  1273.                             }
  1274.  
  1275.                             $listing->setSID($listingId);
  1276.  
  1277.                             SJB_ListingManager::saveListing($listing);
  1278.  
  1279.  
  1280.  
  1281.  
  1282.  
  1283.                             // remove field from temporary storage
  1284.  
  1285.                             $sessionFileStorage = SJB_Session::getValue('tmp_uploads_storage');
  1286.  
  1287.                             if (!empty($sessionFileStorage)) {
  1288.  
  1289.                                 $sessionFileStorage = SJB_Array::unsetValueByPath($sessionFileStorage, "{$formToken}/{$fieldId}");
  1290.  
  1291.                                 SJB_Session::setValue('tmp_uploads_storage', $sessionFileStorage);
  1292.  
  1293.                             }
  1294.  
  1295.                         }
  1296.  
  1297.                     }
  1298.  
  1299.                 }
  1300.  
  1301.  
  1302.  
  1303.                 if (empty($this->errors)) {
  1304.  
  1305.                     echo json_encode( array('result' => 'success') );
  1306.  
  1307.                 } else {
  1308.  
  1309.                     echo json_encode( array('result' => 'error', 'errors' => $this->errors) );
  1310.  
  1311.                 }
  1312.  
  1313.                 exit;
  1314.  
  1315.                 break;
  1316.  
  1317.  
  1318.  
  1319.  
  1320.  
  1321.             ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1322.  
  1323.             case 'get_complexfile_field_data':
  1324.  
  1325.  
  1326.  
  1327.                 $listingId      = SJB_Request::getVar('listing_id', null);
  1328.  
  1329.                 $fieldId        = SJB_Request::getVar('field_id', null);
  1330.  
  1331.                 $listingTypeId  = SJB_Request::getVar('listing_type_id');
  1332.  
  1333.                 $listingTypeSid = SJB_ListingTypeManager::getListingTypeSIDByID($listingTypeId);
  1334.  
  1335.  
  1336.  
  1337.                 $uploadFileManager = new SJB_UploadFileManager();
  1338.  
  1339.  
  1340.  
  1341.                 // replace square brackets in complex field name
  1342.  
  1343.                 $fieldId = str_replace("][", ":", $fieldId);
  1344.  
  1345.                 $fieldId = str_replace("[", ":", $fieldId);
  1346.  
  1347.                 $fieldId = str_replace("]", "", $fieldId);
  1348.  
  1349.  
  1350.  
  1351.  
  1352.  
  1353.                 list($parentField, $subFieldId, $complexStep) = explode(':', $fieldId);
  1354.  
  1355.  
  1356.  
  1357.                 $filesFromTmpStorage = SJB_Session::getValue('tmp_uploads_storage');
  1358.  
  1359.                 //$fileUniqueId = SJB_Array::getPath($filesFromTmpStorage, "listings/{$listingId}/{$fieldId}/file_id");
  1360.  
  1361.                 $fileUniqueId = SJB_Array::getPath($filesFromTmpStorage, "{$formToken}/{$fieldId}/file_id");
  1362.  
  1363.  
  1364.  
  1365.                 // if no temporary files uploaded, return empty string
  1366.  
  1367.                 if (empty($fileUniqueId)) {
  1368.  
  1369.                     return '';
  1370.  
  1371.                 }
  1372.  
  1373.  
  1374.  
  1375.                 // get list of fields for all listing types
  1376.  
  1377.                 $listingTypesInfo = SJB_ListingTypeManager::getAllListingTypesInfo();
  1378.  
  1379.                 $allFields = array();
  1380.  
  1381.                 foreach ($listingTypesInfo as $listingTypeInfo) {
  1382.  
  1383.                     $typeFields = SJB_ListingFieldManager::getListingFieldsInfoByListingType($listingTypeInfo['sid']);
  1384.  
  1385.                     $allFields  = array_merge($allFields, $typeFields);
  1386.  
  1387.                 }
  1388.  
  1389.  
  1390.  
  1391.                 // NEED TO GET COMPLEX SUBFIELD PROPERTY
  1392.  
  1393.                 $commonListingFields = SJB_ListingFieldManager::getCommonListingFieldsInfo();
  1394.  
  1395.                 $listingFieldsByType = $allFields;
  1396.  
  1397.                 $listingFields       = array_merge($commonListingFields, $listingFieldsByType);
  1398.  
  1399.  
  1400.  
  1401.                 // check parent field
  1402.  
  1403.                 $fieldSid = null;
  1404.  
  1405.                 foreach ($listingFields as $field) {
  1406.  
  1407.                     if ($field['id'] != $parentField) {
  1408.  
  1409.                         continue;
  1410.  
  1411.                     }
  1412.  
  1413.                     $fieldSid = $field['sid'];
  1414.  
  1415.                 }
  1416.  
  1417.                 // parent complex field
  1418.  
  1419.                 $complexFieldInfo = SJB_ListingFieldManager::getFieldInfoBySID($fieldSid);
  1420.  
  1421.                 $subFields = SJB_Array::get($complexFieldInfo, 'fields');
  1422.  
  1423.  
  1424.  
  1425.  
  1426.  
  1427.  
  1428.  
  1429.                 if (empty($subFields)) {
  1430.  
  1431.                     echo 'wrong field ID';
  1432.  
  1433.                     exit;
  1434.  
  1435.                 }
  1436.  
  1437.  
  1438.  
  1439.                 // check field for subfield
  1440.  
  1441.                 $complexSubFieldInfo = '';
  1442.  
  1443.                 foreach ($subFields as $subField) {
  1444.  
  1445.                     if ($subField['id'] != $subFieldId) {
  1446.  
  1447.                         continue;
  1448.  
  1449.                     }
  1450.  
  1451.                     $complexSubFieldInfo = $subField;
  1452.  
  1453.                 }
  1454.  
  1455.  
  1456.  
  1457.                 if (empty($complexSubFieldInfo)) {
  1458.  
  1459.                     echo 'Wrong field info';
  1460.  
  1461.                     exit;
  1462.  
  1463.                 }
  1464.  
  1465.  
  1466.  
  1467.                 // OK. COMPLEX SUBFIELD WE HAVE
  1468.  
  1469.                 $complexSubFieldProperty = new SJB_ObjectProperty($complexSubFieldInfo);
  1470.  
  1471.                 // complex file fields contents array of values, not just string filename
  1472.  
  1473.                 $complexSubFieldProperty->setValue( array($complexStep => $fileUniqueId) );
  1474.  
  1475.  
  1476.  
  1477.                 $valueToAssign = $complexSubFieldProperty->getPropertyVariablesToAssign();
  1478.  
  1479.                 $additionalInfo = array(
  1480.  
  1481.                     // and fix to listing_id param
  1482.  
  1483.                     'listing_id' => $listingId,
  1484.  
  1485.                     'listing' => array(
  1486.  
  1487.                         'id' => $listingId,
  1488.  
  1489.                     ),
  1490.  
  1491.                     'complexField' => $parentField,
  1492.  
  1493.                     'complexStep'  => $complexStep,
  1494.  
  1495.                 );
  1496.  
  1497.  
  1498.  
  1499.  
  1500.  
  1501.                 $tp = SJB_System::getTemplateProcessor();
  1502.  
  1503.  
  1504.  
  1505.                 $tp->assign($valueToAssign);
  1506.  
  1507.                 $tp->assign($additionalInfo);
  1508.  
  1509.  
  1510.  
  1511.  
  1512.  
  1513.                 $template = '';
  1514.  
  1515.                 switch ($complexSubFieldProperty->getType()) {
  1516.  
  1517.                     case 'complexfile':
  1518.  
  1519.                         $template = '../field_types/input/file.tpl';
  1520.  
  1521.                         break;
  1522.  
  1523.                     default:
  1524.  
  1525.                         break;
  1526.  
  1527.                 }
  1528.  
  1529.  
  1530.  
  1531.                 $uploadedFilesize = $uploadFileManager->getUploadedFileSize($fileUniqueId);
  1532.  
  1533.                 $filesizeInfo = SJB_HelperFunctions::getFileSizeAndSizeToken($uploadedFilesize);
  1534.  
  1535.                 $tp->assign(array(
  1536.  
  1537.                         'filesize'   => $filesizeInfo['filesize'],
  1538.  
  1539.                         'size_token' => $filesizeInfo['size_token']
  1540.  
  1541.                     )
  1542.  
  1543.                 );
  1544.  
  1545.  
  1546.  
  1547.                 $tp->assign('form_token', $formToken);
  1548.  
  1549.                 $tp->display($template);
  1550.  
  1551.                 break;
  1552.  
  1553.  
  1554.  
  1555.             case 'upload_listing_logo':
  1556.  
  1557.  
  1558.  
  1559.                 $uploadedFieldId = SJB_Request::getVar('uploaded_field_name', '', 'GET');
  1560.  
  1561.                 $listingSid      = SJB_Request::getVar('listing_id', null);
  1562.  
  1563.  
  1564.  
  1565.                 $fieldInfo  = SJB_ListingFieldDBManager::getListingFieldInfoByID($uploadedFieldId);
  1566.  
  1567.                 $tp         = SJB_System::getTemplateProcessor();
  1568.  
  1569.                 $validation = $this->validationManager($fieldInfo, $tp, $uploadedFieldId);
  1570.  
  1571.  
  1572.  
  1573.                 if ($validation === true) {
  1574.  
  1575.                     $upload_manager = new SJB_UploadPictureManager();
  1576.  
  1577.                     $upload_manager->setUploadedFileID($this->fileUniqueId);
  1578.  
  1579.                     $upload_manager->setHeight($fieldInfo['height']);
  1580.  
  1581.                     $upload_manager->setWidth($fieldInfo['width']);
  1582.  
  1583.                     $upload_manager->uploadPicture($fieldInfo['id'], $fieldInfo);
  1584.  
  1585.                     // and set value of file id to property
  1586.  
  1587.                     $this->property->setValue($this->fileUniqueId);
  1588.  
  1589.                     $this->propertyValue = $this->property->getValue();
  1590.  
  1591.  
  1592.  
  1593.                     // for Logo - we already have file_url data and file_thumb data, without file_id
  1594.  
  1595.                     // just add this to session storage
  1596.  
  1597.                     // fill session data for tmp storage
  1598.  
  1599.                     $fieldValue = array(
  1600.  
  1601.                         'file_id'         => $this->fileUniqueId,
  1602.  
  1603.                         'file_url'        => $this->propertyValue['file_url'],
  1604.  
  1605.                         'file_name'       => $this->propertyValue['file_name'],
  1606.  
  1607.                         'thumb_file_url'  => $this->propertyValue['thumb_file_url'],
  1608.  
  1609.                         'thumb_file_name' => $this->propertyValue['thumb_file_name'],
  1610.  
  1611.                     );
  1612.  
  1613.                     $tmpUploadsStorage = SJB_Session::getValue('tmp_uploads_storage');
  1614.  
  1615.                     $tmpUploadsStorage = SJB_Array::setPathValue($tmpUploadsStorage, "{$formToken}/{$uploadedFieldId}", $fieldValue);
  1616.  
  1617.                     SJB_Session::setValue('tmp_uploads_storage', $tmpUploadsStorage);
  1618.  
  1619.  
  1620.  
  1621.                     $tp->assign(array(
  1622.  
  1623.                         'id'    => $uploadedFieldId,
  1624.  
  1625.                         'value' => $fieldValue,
  1626.  
  1627.                     ));
  1628.  
  1629.  
  1630.  
  1631.                 }
  1632.  
  1633.                 $template = '../field_types/input/logo_listing.tpl';
  1634.  
  1635.  
  1636.  
  1637.                 $tp->assign('form_token', $formToken);
  1638.  
  1639.                 $tp->assign('errors', $this->errors);
  1640.  
  1641.                 $tp->assign('listing_id', $listingSid);
  1642.  
  1643.                 $tp->display($template);
  1644.  
  1645.                 break;
  1646.  
  1647.  
  1648.  
  1649.             default:
  1650.  
  1651.                 echo "Action not defined!";
  1652.  
  1653.                 break;
  1654.  
  1655.         }
  1656.  
  1657.  
  1658.  
  1659.         exit;
  1660.  
  1661.  
  1662.  
  1663.     }
  1664.  
  1665.  
  1666.  
  1667.  
  1668.  
  1669.  
  1670.  
  1671.     public static function setTokenDateToSession($token)
  1672.  
  1673.     {
  1674.  
  1675.         $currentTime = time();
  1676.  
  1677.         if (!empty($token)) {
  1678.  
  1679.             $tokensStorage = SJB_Session::getValue('tokens');
  1680.  
  1681.             if (!is_array($tokensStorage)) {
  1682.  
  1683.                 $tokensStorage = array();
  1684.  
  1685.             }
  1686.  
  1687.             $tokensStorage = SJB_Array::setPathValue($tokensStorage, "{$token}", $currentTime);
  1688.  
  1689.             SJB_Session::setValue('tokens', $tokensStorage);
  1690.  
  1691.         }
  1692.  
  1693.     }
  1694.  
  1695.  
  1696.  
  1697.  
  1698.  
  1699.     public static function cleanOldTokensFromSession()
  1700.  
  1701.     {
  1702.  
  1703.         $origTokensStorage = SJB_Session::getValue('tokens');
  1704.  
  1705.         if (!is_array($origTokensStorage)) {
  1706.  
  1707.             return;
  1708.  
  1709.         }
  1710.  
  1711.         $currentTime = time();
  1712.  
  1713.         $expireTime  = 1440; // 24 minutes
  1714.  
  1715.  
  1716.  
  1717.         $tmpUploadsStorage = SJB_Session::getValue('tmp_uploads_storage');
  1718.  
  1719.         $tokensStorage     = $origTokensStorage;
  1720.  
  1721.  
  1722.  
  1723.         // foreach token check time and remove it from session and remove it data from temporary uploads storage
  1724.  
  1725.         foreach ($tokensStorage as $token => $time) {
  1726.  
  1727.             $tokenTime = $currentTime - $time;
  1728.  
  1729.             if ($tokenTime > $expireTime) {
  1730.  
  1731.                 // remove token data from session
  1732.  
  1733.                 $tmpUploadsStorage = SJB_Array::unsetValueByPath($tmpUploadsStorage, "{$token}");
  1734.  
  1735.                 // remove token from tokens list
  1736.  
  1737.                 $origTokensStorage = SJB_Array::unsetValueByPath($origTokensStorage, $token);
  1738.  
  1739.             }
  1740.  
  1741.         }
  1742.  
  1743.         unset($tokensStorage);
  1744.  
  1745.  
  1746.  
  1747.         SJB_Session::setValue('tokens', $origTokensStorage);
  1748.  
  1749.         SJB_Session::setValue('tmp_uploads_storage', $tmpUploadsStorage);
  1750.  
  1751.     }
  1752.  
  1753.    
  1754.  
  1755.     private function validationManager($fieldInfo, $tp, $uploadedFieldId, $complex = null)
  1756.  
  1757.     {
  1758.  
  1759.         // will use tmp_uploads_storage in $_SESSION to storage file info
  1760.  
  1761.         $uniqueStorageId    = SJB_Session::getSessionId();
  1762.  
  1763.         $this->fileUniqueId = $uniqueStorageId . "_" . $uploadedFieldId . "_tmp";
  1764.  
  1765.        
  1766.  
  1767.         // delete uniquie value
  1768.  
  1769.         SJB_UploadFileManager::deleteUploadedFileByID($this->fileUniqueId);
  1770.  
  1771.  
  1772.  
  1773.         $this->property = new SJB_ObjectProperty($fieldInfo);
  1774.  
  1775.         $this->property->setValue('');
  1776.  
  1777.        
  1778.  
  1779.         if ($complex) {
  1780.  
  1781.             $this->property->setComplexParent($complex['parentField']);
  1782.  
  1783.             $this->property->setComplexEnum($complex['complexStep']);
  1784.  
  1785.             $fileNamePath = "{$complex['parentField']}/name/{$complex['subFieldId']}/{$complex['complexStep']}";
  1786.  
  1787.             $fileSizePath = "{$complex['parentField']}/size/{$complex['subFieldId']}/{$complex['complexStep']}";
  1788.  
  1789.         } else {
  1790.  
  1791.             $fileNamePath = $uploadedFieldId . '/name';
  1792.  
  1793.             $fileSizePath = $uploadedFieldId . '/size';
  1794.  
  1795.         }
  1796.  
  1797.  
  1798.  
  1799.         $fileName = SJB_Array::getPath($_FILES, $fileNamePath);
  1800.  
  1801.         if (!$fileName) {
  1802.  
  1803.             $validation = 'UPLOAD_ERR_INI_SIZE';
  1804.  
  1805.         } else {
  1806.  
  1807.             $uploadedFilesize = SJB_Array::getPath($_FILES, $fileSizePath);
  1808.  
  1809.             $filesizeInfo = SJB_HelperFunctions::getFileSizeAndSizeToken($uploadedFilesize);
  1810.  
  1811.             $tp->assign(array(
  1812.  
  1813.                     'filesize'   => $filesizeInfo['filesize'],
  1814.  
  1815.                     'size_token' => $filesizeInfo['size_token']
  1816.  
  1817.                 )
  1818.  
  1819.             );
  1820.  
  1821.            
  1822.  
  1823.             $validation = $this->property->isValid();
  1824.  
  1825.         }
  1826.  
  1827.        
  1828.  
  1829.         $this->propertyValue = $this->property->getValue();
  1830.  
  1831.        
  1832.  
  1833.         if ($validation !== true) {
  1834.  
  1835.             $this->errors[$validation] = 1;
  1836.  
  1837.  
  1838.  
  1839.             if (!$complex) {
  1840.  
  1841.                 $tp->assign(array(
  1842.  
  1843.                     'id'    => $uploadedFieldId,
  1844.  
  1845.                     'value' => array(
  1846.  
  1847.                         'file_url'        => SJB_Array::get($this->propertyValue, 'file_url'),
  1848.  
  1849.                         'file_name'       => SJB_Array::get($this->propertyValue, 'file_name'),
  1850.  
  1851.                         'saved_file_name' => SJB_Array::get($this->propertyValue, 'saved_file_name'),
  1852.  
  1853.                         'file_id'         => $this->fileUniqueId,
  1854.  
  1855.                     ),
  1856.  
  1857.                 ));
  1858.  
  1859.             }
  1860.  
  1861.         }
  1862.  
  1863.  
  1864.  
  1865.         $tp->assign("uploadMaxFilesize", SJB_UploadFileManager::getIniUploadMaxFilesize());
  1866.  
  1867.        
  1868.  
  1869.         return $validation === true;
  1870.  
  1871.     }
  1872.  
  1873.  
  1874.  
  1875.     private function isOwner($listingSid)
  1876.  
  1877.     {
  1878.  
  1879.         $ownerSid       = SJB_ListingManager::getUserSIDByListingSID($listingSid);
  1880.  
  1881.         $currentUserSid = SJB_UserManager::getCurrentUserSID();
  1882.  
  1883.        
  1884.  
  1885.         if ($ownerSid != $currentUserSid
  1886.  
  1887.                 && !SJB_Admin::admin_authed()
  1888.  
  1889.                 && !SJB_SubAdmin::admin_authed()) {
  1890.  
  1891.             return false;
  1892.  
  1893.         }
  1894.  
  1895.        
  1896.  
  1897.         return true;
  1898.  
  1899.     }
  1900.  
  1901. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement