Example #1
0
 public static function CreateEvent($strModule, $strController, $strAction, $data_id = null, $product_id = null)
 {
     if ($strController == "amazon") {
         $MerchantID = _xls_get_conf('AMAZON_MERCHANT_ID');
         $MarketplaceID = _xls_get_conf('AMAZON_MARKETPLACE_ID');
         $MWS_ACCESS_KEY_ID = _xls_get_conf('AMAZON_MWS_ACCESS_KEY_ID');
         $MWS_SECRET_ACCESS_KEY = _xls_get_conf('AMAZON_MWS_SECRET_ACCESS_KEY');
         if (empty($MerchantID) || empty($MarketplaceID) || empty($MWS_ACCESS_KEY_ID) || empty($MWS_SECRET_ACCESS_KEY)) {
             return false;
         }
     }
     //Check to make sure it's not duplicate
     $objTask = TaskQueue::model()->findByAttributes(array('module' => $strModule, 'controller' => $strController, 'action' => $strAction, 'data_id' => $data_id, 'product_id' => $product_id));
     if ($objTask instanceof TaskQueue) {
         return;
     }
     $objTask = new TaskQueue();
     $objTask->module = $strModule;
     $objTask->controller = $strController;
     $objTask->action = $strAction;
     $objTask->data_id = $data_id;
     $objTask->product_id = $product_id;
     if (!$objTask->save()) {
         Yii::log("Error creating Task {$strModule}, {$strController}, {$strAction} " . print_r($objTask->getErrors(), true), 'error', 'application.' . __CLASS__ . "." . __FUNCTION__);
     }
 }
 public function renderWidget($ps_widget_id, &$pa_settings)
 {
     parent::renderWidget($ps_widget_id, $pa_settings);
     $this->opo_view->setVar('request', $this->getRequest());
     $this->opo_view->setVar('hours', $pa_settings['hours']);
     $vo_tq = new TaskQueue();
     $qr_completed = $this->opo_db->query("\n\t\t\t\tSELECT tq.*, u.fname, u.lname \n\t\t\t\tFROM ca_task_queue tq \n\t\t\t\tLEFT JOIN ca_users u ON u.user_id = tq.user_id \n\t\t\t\tWHERE tq.completed_on > ? \n\t\t\t\tORDER BY tq.completed_on desc\n\t\t\t", time() - 60 * 60 * $pa_settings['hours']);
     $va_completed = array();
     while ($qr_completed->nextRow()) {
         $va_row = $qr_completed->getRow();
         $va_params = caUnserializeForDatabase($va_row["parameters"]);
         $va_completed[$va_row["task_id"]]["handler_name"] = $vo_tq->getHandlerName($va_row['handler']);
         $va_completed[$va_row["task_id"]]["created"] = $va_row["created_on"];
         $va_completed[$va_row["task_id"]]["by"] = $va_row["fname"] . ' ' . $va_row['lname'];
         $va_completed[$va_row["task_id"]]["completed_on"] = $va_row["completed_on"];
         $va_completed[$va_row["task_id"]]["error_code"] = $va_row["error_code"];
         if ((int) $va_row["error_code"] > 0) {
             $o_e = new ApplicationError((int) $va_row["error_code"], '', '', '', false, false);
             $va_row["error_message"] = $o_e->getErrorMessage();
         } else {
             $va_row["error_message"] = '';
         }
         $va_completed[$va_row["task_id"]]["error_message"] = $va_row["error_message"];
         if (is_array($va_report = caUnserializeForDatabase($va_row["notes"]))) {
             $va_completed[$va_row["task_id"]]["processing_time"] = (double) $va_report['processing_time'];
         }
         $va_completed[$va_row["task_id"]]["status"] = $vo_tq->getParametersForDisplay($va_row);
     }
     $this->opo_view->setVar('jobs_done', $qr_completed->numRows());
     $this->opo_view->setVar('jobs_done_data', $va_completed);
     $qr_qd = $this->opo_db->query("\n\t\t\t\tSELECT * \n\t\t\t\tFROM ca_task_queue tq\n\t\t\t\tLEFT JOIN ca_users AS u ON tq.user_id = u.user_id\n\t\t\t\tWHERE tq.completed_on is NULL\n\t\t\t");
     $this->opo_view->setVar('jobs_queued_processing', $qr_qd->numRows());
     $va_qd_jobs = array();
     $va_pr_jobs = array();
     while ($qr_qd->nextRow()) {
         $va_row = $qr_qd->getRow();
         $va_params = caUnserializeForDatabase($va_row["parameters"]);
         if (!$vo_tq->rowKeyIsBeingProcessed($va_row["row_key"])) {
             $va_qd_jobs[$va_row["task_id"]]["handler_name"] = $vo_tq->getHandlerName($va_row['handler']);
             $va_qd_jobs[$va_row["task_id"]]["created"] = $va_row["created_on"];
             $va_qd_jobs[$va_row["task_id"]]["by"] = $va_row["fname"] . ' ' . $va_row['lname'];
             $va_qd_jobs[$va_row["task_id"]]["status"] = $vo_tq->getParametersForDisplay($va_row);
         } else {
             $va_pr_jobs[$va_row["task_id"]]["handler_name"] = $vo_tq->getHandlerName($va_row['handler']);
             $va_pr_jobs[$va_row["task_id"]]["created"] = $va_row["created_on"];
             $va_pr_jobs[$va_row["task_id"]]["by"] = $va_row["fname"] . ' ' . $va_row['lname'];
             $va_pr_jobs[$va_row["task_id"]]["status"] = $vo_tq->getParametersForDisplay($va_row);
         }
     }
     $this->opo_view->setVar('qd_job_data', $va_qd_jobs);
     $this->opo_view->setVar('pr_job_data', $va_pr_jobs);
     $this->opo_view->setVar('update_frequency', ($vn_freq = (int) $this->opo_config->get('update_frequency')) ? $vn_freq : 60);
     return $this->opo_view->render('main_html.php');
 }
Example #3
0
 /**
  * @return TaskQueue
  */
 public static function GetInstance()
 {
     if (self::$Instance === null) {
         self::$Instance = new TaskQueue();
     }
     return self::$Instance;
 }
Example #4
0
 /**
  * @param Scalr_Integration_ZohoCrm_Task $task
  * @return void
  */
 private function enqueueTask($task)
 {
     if ($this->taskQueue === null) {
         $this->taskQueue = TaskQueue::Attach(self::TASK_QUEUE_NAME);
     }
     $this->taskQueue->AppendTask($task);
 }
 public function OnStartForking()
 {
     $TaskQueue = TaskQueue::GetInstance();
     $offset = 0;
     while ($Task = $TaskQueue->Peek($offset)) {
         // Find handler that would accept the task
         $handledAndRemoved = false;
         foreach ($this->handlers as $Handler) {
             if ($Handler->Accept($Task)) {
                 // This handler is responsible to make task job
                 try {
                     // Do it dear
                     $Handler->Handle($Task);
                 } catch (Exception $e) {
                     Log::Log(sprintf(_("Task handler fails. %s"), $e->getMessage()), E_USER_ERROR);
                 }
                 // When task has no more targets it's complete
                 if (!$Task->HasActiveTargets()) {
                     // Send report about success/fails
                     try {
                         $Handler->Report($Task);
                     } catch (Exception $e) {
                         Log::Log(sprintf(_("Task report failed. %s"), $e->getMessage()), E_USER_ERROR);
                     }
                     // Remove task from queue
                     $TaskQueue->Remove($Task);
                     $handledAndRemoved = true;
                 }
             }
         }
         if (!$handledAndRemoved) {
             $offset++;
         }
     }
 }
Example #6
0
 function uploadSubmit($p)
 {
     if (!is_url($p['url'])) {
         $error = ErrorHandler::getInstance();
         $error->add('Not an url');
         return false;
     }
     $eventId = TaskQueue::addTask(TASK_FETCH, $p['url']);
     echo '<div class="okay">URL to process has been enqueued.</div><br/>';
     echo ahref('queue/show/' . $eventId, 'Click here') . ' to perform further actions on this file.';
 }
Example #7
0
 function uploadSubmit($p)
 {
     // XhtmlForm:s upload handler har redan processat filen hรคr
     $eventId = TaskQueue::addTask(TASK_UPLOAD, $p['f1']);
     if (!$eventId) {
         echo 'file upload handling failed';
         return false;
     }
     echo '<div class="okay">Your file has been uploaded successfully!</div><br/>';
     echo ahref('queue/show/' . $eventId, 'Click here') . ' to perform further actions on this file.';
     return true;
 }
 /**
  * See if we have any events to fire
  */
 public function actionIndex()
 {
     $criteria = new CDbCriteria();
     $criteria->compare('active', 1);
     $criteria->compare('category', 'CEvent', true, 'AND');
     $objModules = Modules::model()->findAll($criteria);
     foreach ($objModules as $objModule) {
         //Find and our tasks (one of each type this cron cycle)
         $criteria = new CDbCriteria();
         $criteria->select = 'action';
         $criteria->distinct = true;
         $criteria->compare('controller', $objModule->module);
         $objTaskTypes = TaskQueue::model()->findAll($criteria);
         Yii::import('ext.' . $objModule->module . "." . $objModule->module);
         $component = new $objModule->module();
         $component->init();
         //Run init on module first
         foreach ($objTaskTypes as $objType) {
             //Locate a task of this type
             $objTask = TaskQueue::model()->findByAttributes(array('module' => 'integration', 'controller' => $objModule->module, 'action' => $objType->action));
             Yii::log("Found TaskQueue item " . $objTask->controller . " " . $objTask->action, 'info', 'application.' . __CLASS__ . "." . __FUNCTION__);
             $actionName = "OnAction" . ucfirst($objTask->action);
             $objEvent = new CEventTaskQueue(get_class($this), $objTask->data_id, $objTask->product_id);
             Yii::log("Cron action " . $actionName . " on object " . print_r($objEvent, true), 'info', 'application.' . __CLASS__ . "." . __FUNCTION__);
             //Run the action and get a true/false if it was successful
             $retVal = $component->{$actionName}($objEvent);
             if ($retVal) {
                 Yii::log("Successfully processed by Amazon, so deleting task", 'info', 'application.' . __CLASS__ . "." . __FUNCTION__);
                 $objTask->delete();
                 //Successfully ran, so delete entry
             } else {
                 Yii::log("Still waiting on Amazon, will check again next time", 'info', 'application.' . __CLASS__ . "." . __FUNCTION__);
                 $objTask->modified = new CDbExpression('NOW()');
                 $objTask->save();
             }
         }
     }
     //Create a Download Orders event to force any other subsystems to check for new orders
     if (date("i") % 10 == 0) {
         $objEvent = new CEventOrder('CronController', 'onDownloadOrders');
         _xls_raise_events('CEventOrder', $objEvent);
     }
 }
 public function StartThread($data)
 {
     // Reconfigure observers;
     Scalr::ReconfigureObservers();
     //
     // Create pid file
     //
     @file_put_contents(CACHEPATH . "/" . __CLASS__ . ".Daemon.pid", posix_getpid());
     // Get memory usage on start
     $memory_usage = $this->GetMemoryUsage();
     $this->Logger->info("DBQueueEventProcess daemon started. Memory usage: {$memory_usage}M");
     // Get DB instance
     $db = Core::GetDBInstance();
     $FarmObservers = array();
     while (true) {
         // Process tasks from Deferred event queue
         while ($Task = TaskQueue::Attach(QUEUE_NAME::DEFERRED_EVENTS)->Poll()) {
             $Task->Run();
         }
         // Reset task
         TaskQueue::Attach(QUEUE_NAME::DEFERRED_EVENTS)->Reset();
         // Cleaning
         unset($current_memory_usage);
         unset($event);
         // Check memory usage
         $current_memory_usage = $this->GetMemoryUsage() - $memory_usage;
         if ($current_memory_usage > $this->DaemonMemoryLimit) {
             $this->Logger->warn("DBQueueEventProcess daemon reached memory limit {$this->DaemonMemoryLimit}M, Used:{$current_memory_usage}M");
             $this->Logger->warn("Restart daemon.");
             exit;
         }
         // Sleep for 60 seconds
         sleep(15);
         // Clear stat file cache
         clearstatcache();
         // Check daemon file for modifications.
         if ($this->DaemonMtime && $this->DaemonMtime < @filemtime(__FILE__)) {
             $this->Logger->warn(__FILE__ . " - updated. Exiting for daemon reload.");
             exit;
         }
     }
 }
Example #10
0
 public function crawl($loop = true)
 {
     if ($this->config['clear_dir']) {
         if (file_exists($this->config['dir'])) {
             $res = @deldir($this->config['dir']);
             if (!$res) {
                 throw new \Exception('clear directory:"' . $this->config['dir'] . '" failed');
             }
         }
     }
     if (!file_exists($this->config['dir'])) {
         $res = @mkdir($this->config['dir'], 0755);
         if (!$res) {
             throw new \Exception('create directory:"' . $this->config['dir'] . '" failed');
         }
     }
     $seed = TaskFactory::init($this->base_url, $this->config);
     $queue = TaskQueue::getInstance();
     $queue->push($seed);
     $loop_count = 0;
     while ($task = $queue->shift()) {
         Logger::info('ๅผ€ๅง‹ไปปๅŠก: ' . $task['path']);
         $task->download();
         // break;
         if (gettype($loop) == 'boolean' && !$loop || is_numeric($loop) && $loop < $loop_count++ || in_array($task['ext'], $this->config['parse_ignore_ext'])) {
             continue;
         }
         $urls = $task->parse();
         foreach ($urls as $url) {
             $new = TaskFactory::create($url, $task);
             if ($new) {
                 $queue->push($new);
             }
         }
     }
 }
                 $err[] = $e->getMessage();
             }
         }
     }
     if (!$err) {
         if (!($post_registrant || $post_admin || $post_billing || $post_tech)) {
             $errmsg = sprintf(_("No contacts were selected"));
         } else {
             if (!$_SESSION['BU_TLD']) {
                 $errmsg = sprintf(_("No domain extension was selected"));
             } else {
                 if (!$_SESSION['BU_DOMAINS']) {
                     $errmsg = sprintf(_("No domains were selected"));
                 } else {
                     // Create bulk update task abd put in into tasks queue
                     $Queue = TaskQueue::GetInstance();
                     try {
                         // Define contact clids list
                         $clids = array(CONTACT_TYPE::REGISTRANT => $post_registrant, CONTACT_TYPE::ADMIN => $post_admin, CONTACT_TYPE::TECH => $post_tech, CONTACT_TYPE::BILLING => $post_billing);
                         // Construct task
                         $Task = new Task($_SESSION['userid'], new BulkUpdateContactJob($_SESSION['BU_TLD'], $clids), $_SESSION['BU_DOMAINS']);
                         $Queue->Put($Task);
                         CoreUtils::Redirect("bulk_update_complete.php");
                     } catch (Exception $e) {
                         $errmsg = $e->getMessage();
                     }
                 }
             }
         }
     }
 }
 protected function CreateTask()
 {
     try {
         // Create task and enqueue it in epp-drs system queue.
         // Task will be handled in TaskQueueProcess.
         $Task = new Task($_SESSION['userid'], new BulkRegisterDomainJob($this->GetTLDs(), $this->periods, $this->contact_list, $this->ns, $this->extra), $this->domains);
         $Queue = TaskQueue::GetInstance();
         $Queue->Put($Task);
         CoreUtils::Redirect("bulk_update_complete.php");
     } catch (Exception $e) {
         $this->display["err"] = $e->getMessage();
     }
 }
Example #13
0
<?php

/**
 * This script removes old files from the process server
 */
die;
// FIXME script is nonfunctional!
require_once 'find_config.php';
define('EXPIRE_DAYS', 90);
//removes source media
$list = getProcessQueue(PROCESS_FETCH, '', ORDER_COMPLETED, EXPIRE_DAYS);
foreach ($list as $row) {
    //$h->files->deleteFile($row['refererId'], 0, true);
}
//removes converted media - FIXME lookup is wierd
$list = getProcessQueue(PROCESS_CONVERT_TO_DEFAULT, '', ORDER_COMPLETED, EXPIRE_DAYS);
foreach ($list as $row) {
    TaskQueue::getEntry($row['refererId']);
    //$h->files->deleteFile($row['refererId'], 0, true);
}
 /**
  * 
  *
  * @param array $pa_options Array of options passed through to _initView 
  */
 public function ChangeType($pa_options = null)
 {
     if (!is_array($pa_options)) {
         $pa_options = array();
     }
     list($vn_set_id, $t_set, $t_subject, $t_ui) = $this->_initView($pa_options);
     if (!$vn_set_id) {
         $this->response->setRedirect($this->request->config->get('error_display_url') . '/n/3200?r=' . urlencode($this->request->getFullUrlPath()));
         return;
     }
     // Can user batch edit this table?
     if (!$this->request->user->canDoAction('can_batch_edit_' . $t_set->getAppDatamodel()->getTableName($t_set->get('table_num')))) {
         $this->response->setRedirect($this->request->config->get('error_display_url') . '/n/3210?r=' . urlencode($this->request->getFullUrlPath()));
         return;
     }
     if (!$this->request->user->canDoAction("can_change_type_" . $t_subject->tableName()) || !method_exists($t_subject, "getTypeList")) {
         $this->response->setRedirect($this->request->config->get('error_display_url') . '/n/3260?r=' . urlencode($this->request->getFullUrlPath()));
         return;
     }
     $vn_new_type_id = $this->request->getParameter('new_type_id', pInteger);
     $va_type_list = $t_subject->getTypeList();
     if (!isset($va_type_list[$vn_new_type_id])) {
         $this->response->setRedirect($this->request->config->get('error_display_url') . '/n/3260?r=' . urlencode($this->request->getFullUrlPath()));
         return;
     }
     $va_last_settings = array('set_id' => $vn_set_id, 'screen' => $this->request->getActionExtra(), 'user_id' => $this->request->getUserID(), 'values' => $_REQUEST, 'sendMail' => (bool) $this->request->getParameter('send_email_when_done', pInteger), 'sendSMS' => (bool) $this->request->getParameter('send_sms_when_done', pInteger));
     if ((bool) $this->request->config->get('queue_enabled') && (bool) $this->request->getParameter('run_in_background', pInteger)) {
         // queue for background processing
         $o_tq = new TaskQueue();
         $vs_row_key = $vs_entity_key = join("/", array($this->request->getUserID(), $t_set->getPrimaryKey(), time(), rand(1, 999999)));
         if (!$o_tq->addTask('batchEditor', array_merge($va_last_settings, array('isBatchTypeChange' => true, 'new_type_id' => $vn_new_type_id)), array("priority" => 100, "entity_key" => $vs_entity_key, "row_key" => $vs_row_key, 'user_id' => $this->request->getUserID()))) {
             //$this->postError(100, _t("Couldn't queue batch processing for"),"EditorContro->_processMedia()");
         }
         $this->render('editor/batch_queued_html.php');
     } else {
         // run now
         $app = AppController::getInstance();
         $app->registerPlugin(new BatchEditorProgress($this->request, $t_set, $t_subject, array('type_id' => $vn_new_type_id, 'isBatchTypeChange' => true, 'sendMail' => (bool) $this->request->getParameter('send_email_when_done', pInteger), 'sendSMS' => (bool) $this->request->getParameter('send_sms_when_done', pInteger), 'runInBackground' => (bool) $this->request->getParameter('run_in_background', pInteger))));
         $this->render('editor/batch_results_html.php');
     }
     $this->request->user->setVar('batch_editor_last_settings', $va_last_settings);
 }
 /**
  * Product lookup and optional delete, shows inventory numbers
  */
 public function actionProducts()
 {
     if (isset($_POST['pk']) && isset($_POST['name']) && isset($_POST['value'])) {
         if ($_POST['name'] == 'code' && $_POST['value'] == "") {
             $items = CartItem::model()->findAll("product_id=" . $_POST['pk'] . " AND (cart_type=" . CartType::order . " OR cart_type=" . CartType::awaitpayment . ")");
             if ($items) {
                 echo "You cannot delete a product that has been used on an order";
             } else {
                 _dbx("set foreign_key_checks=0;");
                 Product::model()->updateAll(array('image_id' => null), 'id =' . $_POST['pk']);
                 Images::model()->deleteAllByAttributes(array('product_id' => $_POST['pk']));
                 ProductCategoryAssn::model()->deleteAllByAttributes(array('product_id' => $_POST['pk']));
                 ProductRelated::model()->deleteAllByAttributes(array('product_id' => $_POST['pk']));
                 ProductRelated::model()->deleteAllByAttributes(array('related_id' => $_POST['pk']));
                 ProductTags::model()->deleteAllByAttributes(array('product_id' => $_POST['pk']));
                 ProductQtyPricing::model()->deleteAllByAttributes(array('product_id' => $_POST['pk']));
                 ProductText::model()->deleteAllByAttributes(array('product_id' => $_POST['pk']));
                 WishlistItem::model()->deleteAllByAttributes(array('product_id' => $_POST['pk']));
                 TaskQueue::model()->deleteAllByAttributes(array('product_id' => $_POST['pk']));
                 Product::model()->deleteByPk($_POST['pk']);
                 _dbx("set foreign_key_checks=1;");
                 echo "delete";
             }
         } else {
             echo Yii::t('admin', 'You cannot change a product code here. Delete the code to remove it manually from the Web Store database');
         }
     } else {
         $model = new Product();
         if (isset($_GET['q'])) {
             $model->code = $_GET['q'];
         }
         $this->render("products", array('model' => $model));
     }
 }
Example #16
0
<?php

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
require_once "internals.php";
require_once "lib/RetriggerController.php";
require_once "lib/DB/TaskQueue.php";
require_once "lib/DB/QueuedTask.php";
require_once "lib/Slack.php";
init_database();
if ($unit = GET_int("unit")) {
    $queue = new TaskQueue($unit);
    if ($queue->has_active_task()) {
        Slack::log("requesting new task, while old task is still running!");
        $task = $queue->get_active_task();
        $task->reportError("Requested new task, while this task was still running.");
    }
    if (!$queue->has_queued_tasks()) {
        if (!RetriggerController::fillQueue($unit)) {
            die("No tasks to schedule");
        }
    }
    $task = $queue->get_oldest_available_queued_task();
    if (!$task) {
        echo json_encode(array("task" => "sleep 60", "id" => 0));
        die;
    }
    $task->setStarted();
    echo json_encode(array("task" => $task->task(), "id" => $task->id));
    die;
Example #17
0
 /**
  * Process queued tasks
  */
 public static function process_task_queue($po_opts = null)
 {
     require_once __CA_LIB_DIR__ . "/core/TaskQueue.php";
     $vo_tq = new TaskQueue();
     if (!$po_opts->getOption("quiet")) {
         CLIUtils::addMessage(_t("Processing queued tasks..."));
     }
     $vo_tq->processQueue();
     // Process queued tasks
     if (!$po_opts->getOption("quiet")) {
         CLIUtils::addMessage(_t("Processing recurring tasks..."));
     }
     $vo_tq->runPeriodicTasks();
     // Process recurring tasks implemented in plugins
     if (!$po_opts->getOption("quiet")) {
         CLIUtils::addMessage(_t("Processing complete."));
     }
     return true;
 }
Example #18
0
 /**
  * This function will run parse an order that we get from Amazon MWS.
  * It saves orders of the customers to the DB.
  * @param $response ListOrderItemsResponse Contains the orders from Amazon
  * Marketplace WebService
  * @return void
  */
 public function parseListOrders($response)
 {
     $checkDate = date("Y-m-d", strtotime($this->amazon_check_time));
     $listOrdersResult = $response->getListOrdersResult();
     if ($listOrdersResult->isSetOrders()) {
         $orders = $listOrdersResult->getOrders();
         $orderList = $orders->getOrder();
         foreach ($orderList as $order) {
             if ($order->isSetAmazonOrderId()) {
                 $strOrderId = $order->getAmazonOrderId();
                 Yii::log("Found Amazon Order " . $strOrderId, 'info', 'application.' . __CLASS__ . "." . __FUNCTION__);
                 $objCart = Cart::LoadByIdStr($strOrderId);
                 if (!$objCart instanceof Cart) {
                     //We ignore orders we've already downloaded
                     $objCart = new Cart();
                     $objCart->id_str = $strOrderId;
                     $objCart->origin = 'amazon';
                     //We mark this as just a cart, not an order, because we download the items next
                     $objCart->cart_type = CartType::cart;
                     $objOrderTotal = $order->getOrderTotal();
                     Yii::log("Order total information " . print_r($objOrderTotal, true), 'info', 'application.' . __CLASS__ . "." . __FUNCTION__);
                     $objCart->total = $objOrderTotal->getAmount();
                     $objCart->currency = $objOrderTotal->getCurrencyCode();
                     $objCart->status = OrderStatus::Requested;
                     $objCart->datetime_cre = $order->getPurchaseDate();
                     $objCart->modified = $order->getLastUpdateDate();
                     if (!$objCart->save()) {
                         Yii::log("Error saving cart " . print_r($objCart->getErrors(), true), 'error', 'application.' . __CLASS__ . "." . __FUNCTION__);
                     }
                     //Since email from is Anonymous, we probably will have to create a shell record
                     $objCustomer = Customer::LoadByEmail($order->getBuyerEmail());
                     if (!$objCustomer) {
                         $customerName = $this->_getCustomerName($order->getBuyerName());
                         $objCustomer = new Customer();
                         $objCustomer->email = $order->getBuyerEmail();
                         $objCustomer->first_name = $customerName['first_name'];
                         $objCustomer->last_name = $customerName['last_name'];
                         $objCustomer->record_type = Customer::EXTERNAL_SHELL_ACCOUNT;
                         $objCustomer->allow_login = Customer::UNAPPROVED_USER;
                         $objCustomer->save();
                     }
                     $objCart->customer_id = $objCustomer->id;
                     if (!$objCart->save()) {
                         Yii::log("Error saving cart " . print_r($objCart->getErrors(), true), 'error', 'application.' . __CLASS__ . "." . __FUNCTION__);
                     }
                     if ($order->isSetShippingAddress()) {
                         $shippingAddress = $order->getShippingAddress();
                         $countrycode = Country::IdByCode($shippingAddress->getCountryCode());
                         if ($shippingAddress->isSetStateOrRegion()) {
                             $objState = State::LoadByCode($shippingAddress->getStateOrRegion(), $countrycode);
                         }
                         $customerName = $this->_getCustomerName($shippingAddress->getName());
                         $config = array('address_label' => 'amazon', 'customer_id' => $objCustomer->id, 'first_name' => $customerName['first_name'], 'last_name' => $customerName['last_name'], 'address1' => $shippingAddress->getAddressLine1(), 'address2' => trim($shippingAddress->getAddressLine2() . " " . $shippingAddress->getAddressLine3()), 'city' => $shippingAddress->getCity(), 'state_id' => $objState->id, 'postal' => $shippingAddress->getPostalCode(), 'country_id' => $countrycode, 'phone' => $shippingAddress->getPhone());
                         $objCustAddress = CustomerAddress::findOrCreate($config);
                         $objCustomer->default_billing_id = $objCustAddress->id;
                         $objCustomer->default_shipping_id = $objCustAddress->id;
                         $objCustomer->save();
                         $objCart->shipaddress_id = $objCustAddress->id;
                         $objCart->billaddress_id = $objCustAddress->id;
                         //Amazon doesn't provide billing data, just dupe
                         if (!$objCart->save()) {
                             Yii::log("Error saving cart " . print_r($objCart->getErrors(), true), 'error', 'application.' . __CLASS__ . "." . __FUNCTION__);
                         }
                         Yii::log("Looking for destination " . $objState->country_code . " " . $objState->code . " " . $shippingAddress->getPostalCode(), 'info', 'application.' . __CLASS__ . "." . __FUNCTION__);
                         $objDestination = Destination::LoadMatching($objState->country_code, $objState->code, $shippingAddress->getPostalCode());
                         if ($objDestination === null) {
                             Yii::log("Did not find destination, using default in Web Store ", 'info', 'application.' . __CLASS__ . "." . __FUNCTION__);
                             $objDestination = Destination::getAnyAny();
                         }
                         $objCart->tax_code_id = $objDestination->taxcode;
                         $objCart->recalculateAndSave();
                     }
                     if ($order->isSetShipServiceLevel()) {
                         $strShip = $order->getShipServiceLevel();
                         //If we have a shipping object already, update it, otherwise create it
                         if (isset($objCart->shipping)) {
                             $objShipping = $objCart->shipping;
                         } else {
                             //create
                             $objShipping = new CartShipping();
                             if (!$objShipping->save()) {
                                 Yii::log("Error saving shipping info for cart " . print_r($objShipping->getErrors(), true), 'error', 'application.' . __CLASS__ . "." . __FUNCTION__);
                             }
                         }
                         if ($order->isSetShipmentServiceLevelCategory()) {
                             $strShip = $order->getShipmentServiceLevelCategory();
                         }
                         $objShipping->shipping_module = get_class($this);
                         $objShipping->shipping_data = $strShip;
                         $objShipping->shipping_method = $this->objModule->getConfig('product');
                         $objShipping->shipping_cost = 0;
                         $objShipping->shipping_sell = 0;
                         $objShipping->save();
                         $objCart->shipping_id = $objShipping->id;
                         if (!$objCart->save()) {
                             Yii::log("Error saving cart " . print_r($objCart->getErrors(), true), 'error', 'application.' . __CLASS__ . "." . __FUNCTION__);
                         }
                     }
                     //Because Amazon comes down with no payment info, just generate one here
                     $objP = new CartPayment();
                     $objP->payment_method = $this->objModule->getConfig('ls_payment_method');
                     $objP->payment_module = get_class($this);
                     $objP->payment_data = 'Amazon';
                     $objP->payment_amount = $objOrderTotal->getAmount();
                     $objP->datetime_posted = $order->getPurchaseDate();
                     if (!$objP->save()) {
                         Yii::log("Error saving payment " . print_r($objP->getErrors(), true), 'error', 'application.' . __CLASS__ . "." . __FUNCTION__);
                     }
                     $objCart->payment_id = $objP->id;
                     if (!$objCart->save()) {
                         Yii::log("Error saving cart " . print_r($objCart->getErrors(), true), 'error', 'application.' . __CLASS__ . "." . __FUNCTION__);
                     }
                     TaskQueue::CreateEvent('integration', get_class($this), 'ListOrderDetails', $objCart->id_str . "," . $checkDate);
                 }
             }
         }
     }
 }
Example #19
0
 /**
  * Store event in database
  *
  * @param integer $farmid
  * @param string $event_name
  */
 public static function StoreEvent($farmid, Event $event)
 {
     if ($event->SkipDeferredOperations) {
         return true;
     }
     try {
         $DB = Core::GetDBInstance();
         // Get Smarty object
         $Smarty = Core::GetSmartyInstance();
         // Assign vars
         $Smarty->assign(array("event" => $event));
         // Generate event message
         if (file_exists(CF_TEMPLATES_PATH . "/event_messages/{$event->GetName()}.tpl")) {
             $message = $Smarty->fetch("event_messages/{$event->GetName()}.tpl");
             $short_message = $Smarty->fetch("event_messages/{$event->GetName()}.short.tpl");
             // Store event in database
             $DB->Execute("INSERT INTO events SET \r\n\t\t\t\t\t\tfarmid\t= ?, \r\n\t\t\t\t\t\ttype\t= ?, \r\n\t\t\t\t\t\tdtadded\t= NOW(),\r\n\t\t\t\t\t\tmessage\t= ?,\r\n\t\t\t\t\t\tshort_message = ?, \r\n\t\t\t\t\t\tevent_object = ?,\r\n\t\t\t\t\t\tevent_id\t = ?\r\n\t\t\t\t\t\t", array($farmid, $event->GetName(), $message, $short_message, serialize($event), $event->GetEventID()));
             $eventid = $DB->Insert_ID();
             // Add task for fire deferred event
             TaskQueue::Attach(QUEUE_NAME::DEFERRED_EVENTS)->AppendTask(new FireDeferredEventTask($eventid));
         } else {
             Logger::getLogger(__CLASS__)->warn(sprintf(_("Cannot store event in database: '{$event->GetName()}' message template not found")));
         }
     } catch (Exception $e) {
         Logger::getLogger(__CLASS__)->fatal(sprintf(_("Cannot store event in database: %s"), $e->getMessage()));
     }
 }
Example #20
0
while ($unit = mysql_fetch_object($qUnits)) {
    echo "<tr><td>" . $unit->id;
    $qTasks = mysql_query("SELECT * FROM control_tasks WHERE control_unit_id =" . $unit->id) or die(mysql_error());
    if (mysql_num_rows($qTasks) == 0) {
        echo "<td>/";
    } else {
        echo "<td>";
        while ($task = mysql_fetch_object($qTasks)) {
            $machine = Machine::FromId($task->machine_id);
            $mode = Mode::FromId($task->mode_id);
            echo $machine->description();
            echo $mode ? " with " . $mode->name() : "";
            echo "<br>";
        }
    }
    $queue = new TaskQueue($unit->id);
    echo "<td>";
    if ($queue->has_active_task()) {
        $active = $queue->get_active_task();
        echo "running";
        echo "<span title='" . date("G:i d/m/Y", $active->start_time()) . "'> started " . time_ago($active->start_time()) . " ago</span>";
    } else {
        echo "not running";
    }
    echo "<td>";
    if ($queue->has_queued_tasks()) {
        $tasks = $queue->get_queued_tasks();
        $count = count($tasks);
        echo $count . " tasks";
        if ($count > 0 && !$queue->has_active_task()) {
            $min = $tasks[0]->available_time();
/**
 * Takes some work orders from the process queue and performs them
 */
function processQueue()
{
    global $db, $config;
    //Only allows a few work orders being executed at once, so we can do this very often
    if (TaskQueue::getTaskQueueStatusCnt(ORDER_EXECUTING) >= $config['process']['process_limit']) {
        echo "TOO MUCH ACTIVE WORK, ABORTING\n";
        return;
    }
    $job = TaskQueue::getOldestEntry();
    if (!$job) {
        return;
    }
    //mark current job as "IN PROGRESS" so another process won't start on it aswell
    TaskQueue::markTask($job['entryId'], ORDER_EXECUTING);
    echo "\n\n-------------\n";
    switch ($job['orderType']) {
        case TASK_IMAGE_RECODE:
            echo 'IMAGE RECODE<br/>';
            if (!in_array($job['orderParams'], $h->files->image_mime_types)) {
                echo 'error: invalid mime type<br/>';
                $h->session->log('Process queue error - image conversion destination mimetype not supported: ' . $job['orderParams'], LOGLEVEL_ERROR);
                break;
            }
            $newId = $h->files->cloneFile($job['referId'], FILETYPE_CLONE_CONVERTED);
            $exec_start = microtime(true);
            $check = convertImage($h->files->findUploadPath($job['referId']), $h->files->findUploadPath($newId), $job['orderParams']);
            $exec_time = microtime(true) - $exec_start;
            echo 'Execution time: ' . shortTimePeriod($exec_time) . '<br/>';
            if (!$check) {
                $h->session->log('#' . $job['entryId'] . ': IMAGE CONVERT failed! format=' . $job['orderParams'], LOGLEVEL_ERROR);
                echo 'Error: Image convert failed!<br/>';
                break;
            }
            $h->files->updateFile($newId, $job['orderParams']);
            markQueueCompleted($job['entryId'], $exec_time);
            break;
        case TASK_AUDIO_RECODE:
            //Recodes source audio file into orderParams destination format
            $dst_audio_ok = array('ogg', 'wma', 'mp3');
            //FIXME: config item or $h->files->var
            if (!in_array($job['orderParams'], $dst_audio_ok)) {
                echo 'error: invalid mime type<br/>';
                $h->session->log('Process queue error - audio conversion destination mimetype not supported: ' . $job['orderParams'], LOGLEVEL_ERROR);
                break;
            }
            $file = $h->files->getFileInfo($job['referId']);
            if (!$file) {
                echo 'Error: no fileentry existed for fileId ' . $job['referId'];
                break;
            }
            $newId = $h->files->cloneFile($job['referId'], FILETYPE_CLONE_CONVERTED);
            echo 'Recoding source audio of "' . $file['fileName'] . '" (' . $file['fileMime'] . ') to format ' . $job['orderParams'] . " ...\n";
            switch ($job['orderParams']) {
                case 'application/x-ogg':
                    //FIXME hur anger ja dst-format utan filรคndelse? tvingas gรถra det i 2 steg nu
                    $dst_file = 'tmpfile.ogg';
                    $c = '/usr/local/bin/ffmpeg -i "' . $h->files->findUploadPath($job['referId']) . '" ' . $dst_file;
                    break;
                case 'audio/x-ms-wma':
                    $dst_file = 'tmpfile.wma';
                    $c = '/usr/local/bin/ffmpeg -i "' . $h->files->findUploadPath($job['referId']) . '" ' . $dst_file;
                    break;
                case 'audio/mpeg':
                case 'audio/x-mpeg':
                    //fixme: source & destination should not be able to be the same!
                    $dst_file = 'tmpfile.mp3';
                    $c = '/usr/local/bin/ffmpeg -i "' . $h->files->findUploadPath($job['referId']) . '" ' . $dst_file;
                    break;
                default:
                    die('unknown destination audio format: ' . $job['orderParams']);
            }
            echo 'Executing: ' . $c . "\n";
            $exec_time = exectime($c);
            echo 'Execution time: ' . shortTimePeriod($exec_time) . "\n";
            if (!file_exists($dst_file)) {
                echo '<b>FAILED - dst file ' . $dst_file . " dont exist!\n";
                break;
            }
            //FIXME: behรถver inget rename-steg. kan skriva till rรคtt output fil i fรถrsta steget
            rename($dst_file, $h->files->upload_dir . $newId);
            $h->files->updateFile($newId);
            markQueueCompleted($job['entryId'], $exec_time);
            break;
        case TASK_VIDEO_RECODE:
            echo "VIDEO RECODE:\n";
            $exec_start = microtime(true);
            if (convertVideo($job['referId'], $job['orderParams']) === false) {
                markQueue($job['entryId'], ORDER_FAILED);
            } else {
                markQueueCompleted($job['entryId'], microtime(true) - $exec_start);
            }
            break;
        case TASK_FETCH:
            echo "FETCH CONTENT\n";
            $fileName = basename($job['orderParams']);
            //extract filename part of url, used as "filename" in database
            $http = new HttpClient($job['orderParams']);
            $http->getHead();
            if ($http->getStatus() != 200) {
                // retry in 20 seconds if file is not yet ready
                retryQueueEntry($job['entryId'], 20);
                break;
            }
            $newFileId = FileList::createEntry(FILETYPE_PROCESS, 0, 0, $fileName);
            $c = 'wget ' . escapeshellarg($job['orderParams']) . ' -O ' . FileInfo::getUploadPath($newFileId);
            echo "\$ " . $c . "\n";
            $retval = 0;
            $exec_time = exectime($c, $retval);
            if (!$retval) {
                //TODO: process html document for media links if it is a html document
                TaskQueue::markTaskCompleted($job['entryId'], $exec_time, $newFileId);
                FileInfo::updateData($newFileId);
            } else {
                //wget failed somehow, delay work for 1 minute
                retryQueueEntry($job['entryId'], 60);
                $files->deleteFile($newFileId, 0, true);
                //remove failed local file entry
            }
            break;
        case TASK_CONVERT_TO_DEFAULT:
            echo "CONVERT TO DEFAULT\n";
            //referId is entryId of previous proccess queue order
            $params = unserialize($job['orderParams']);
            $prev_job = TaskQueue::getEntry($job['referId']);
            if ($prev_job['orderStatus'] != ORDER_COMPLETED) {
                retryQueueEntry($job['entryId'], 60);
                break;
            }
            $file = $files->getFileInfo($prev_job['referId']);
            $exec_start = microtime(true);
            $newId = false;
            switch ($file['mediaType']) {
                case MEDIATYPE_VIDEO:
                    $newId = convertVideo($prev_job['referId'], $h->files->default_video, !empty($params['callback']) ? false : true, !empty($params['watermark']) ? $params['watermark'] : '');
                    break;
                case MEDIATYPE_AUDIO:
                    $newId = convertAudio($prev_job['referId'], $h->files->default_audio);
                    break;
                default:
                    echo "UNKNOWN MEDIA TYPE " . $file['mediaType'] . ", MIME TYPE " . $file['fileMime'] . ", CANNOT CONVERT MEDIA!!!\n";
                    break;
            }
            if (!$newId) {
                markQueue($job['entryId'], ORDER_FAILED);
                return false;
            }
            markQueueCompleted($job['entryId'], microtime(true) - $exec_start);
            if (empty($params['callback'])) {
                break;
            }
            //'uri' isnt known before the new file is created so it is added at this point
            $uri = $config['core']['full_url'] . 'api/file.php?id=' . $newId;
            $params['callback'] .= (strpos($params['callback'], '?') !== false ? '&' : '?') . 'uri=' . urlencode($uri);
            $data = file_get_contents($params['callback']);
            echo "Performing callback: " . $params['callback'] . "\n\n";
            echo "Callback script returned:\n" . $data;
            storeCallbackData($job['entryId'], $data, $params);
            break;
        default:
            echo "Unknown ordertype: " . $job['orderType'] . "\n";
            d($job);
            die;
    }
}
Example #22
0
 /**
  * Method invoked when the task queue needs to actually execute the task. For mediaproc this means
  * actually doing the processing of media!
  *
  * Return false on failure/error and sets the error property with an error description. Returns an array
  * with processing details on success.
  *
  * @param array $pa_parameters An unserialized parameters array for the current task (eg. unserialized data from ca_task_queue.parameters)
  * @return array Returns false on error, or an array with processing details on success
  */
 public function process($pa_parameters)
 {
     $vs_table = $pa_parameters["TABLE"];
     // name of table of record we're processing
     $vs_field = $pa_parameters["FIELD"];
     // name of field in record we're processing
     $vs_pk = $pa_parameters["PK"];
     // Field name of primary key of record we're processing
     $vn_id = $pa_parameters["PK_VAL"];
     // Value of primary key
     $vs_input_mimetype = $pa_parameters["INPUT_MIMETYPE"];
     // Mimetype of input file
     $vs_input_file = $pa_parameters["FILENAME"];
     // Full path to input file
     // Array of media versions to process; keys are version names,
     // values are arrays with info about processing for that version
     // Currently the info array contains a single key, 'VOLUME', which indicates
     // the volume the version should be written to
     $va_versions = $pa_parameters["VERSIONS"];
     $va_options = $pa_parameters["OPTIONS"];
     // Array of processing options; names of options to employ are keys, settings are values
     // If true, then input media is *not* deleted
     $vb_dont_delete_original_media = (bool) $pa_parameters["DONT_DELETE_OLD_MEDIA"];
     $va_report = array('errors' => array(), 'notes' => array());
     $o_dm = Datamodel::load();
     $o_media_volumes = new MediaVolumes();
     $o_media = new Media();
     $o_media_proc_settings = new MediaProcessingSettings($vs_table, $vs_field);
     $vs_media_type = $o_media_proc_settings->canAccept($vs_input_mimetype);
     $va_version_info = $o_media_proc_settings->getMediaTypeVersions($vs_media_type);
     if (!file_exists($vs_input_file)) {
         $o_eventlog = new EventLog();
         $o_eventlog->log(array("CODE" => "DEBG", "SOURCE" => "TaskQueue->mediaproc->process()", "MESSAGE" => "Record {$vs_table}.field = file '{$vs_input_file}' did not exist; queued file was discarded"));
         $va_report['errors'][] = _t("Record %1.field = file '%2' did not exist; queued file was discarded", $vs_table, $vs_input_file);
         return $va_report;
     }
     if ($t_instance = $o_dm->getInstanceByTableName($vs_table, true)) {
         if ($t_instance->hasField($vs_field)) {
             if (!$t_instance->load($vn_id)) {
                 # record no longer exists
                 if (!$vb_dont_delete_original_media) {
                     @unlink($vs_input_file);
                 }
                 $o_eventlog = new EventLog();
                 $o_eventlog->log(array("CODE" => "DEBG", "SOURCE" => "TaskQueue->mediaproc->process()", "MESSAGE" => "Record {$vs_table}.field = {$vn_id} did not exist; queued file was discarded"));
                 $o_media->cleanup();
                 $va_report['errors'][] = _t("Record %1.field = %2 did not exist; queued file was discarded", $vs_table, $vn_id);
                 return $va_report;
             }
         } else {
             # bad field name
             $this->error->setError(551, _t("Invalid media field '%1' in table '%2'", $vs_field, $vs_table), "mediaproc->process()");
             return false;
         }
     } else {
         # bad table name
         $this->error->setError(550, _t("Invalid media field table '%1'", $vs_table), "mediaproc->process()");
         return false;
     }
     $va_old_media_to_delete = array();
     foreach ($va_versions as $v => $va_version_settings) {
         $vs_use_icon = null;
         if (!file_exists($vs_input_file)) {
             $this->error->setError(505, _t("Input media file '%1' does not exist", $vs_input_file), "mediaproc->process()");
             $o_media->cleanup();
             return false;
         }
         if (!is_readable($vs_input_file)) {
             $this->error->setError(506, _t("Denied permission to read input media file '%1'", $vs_input_file), "mediaproc->process()");
             $o_media->cleanup();
             return false;
         }
         if (!$o_media->read($vs_input_file)) {
             $this->error->setError(1600, _t("Could not process input media file '%1': %2", $vs_input_file, join('; ', $o_media->getErrors())), "mediaproc->process()");
             $o_media->cleanup();
             return false;
         }
         $vs_rule = isset($va_version_info[$v]['RULE']) ? $va_version_info[$v]['RULE'] : '';
         $va_rules = $o_media_proc_settings->getMediaTransformationRule($vs_rule);
         $va_volume_info = $o_media_volumes->getVolumeInformation($va_version_settings['VOLUME']);
         if (sizeof($va_rules) == 0) {
             $vs_output_mimetype = $vs_input_mimetype;
             #
             # don't process this media, just copy the file
             #
             $vs_ext = $o_media->mimetype2extension($vs_output_mimetype);
             if (!$vs_ext) {
                 $this->error->setError(1600, _t("File could not be copied for %1; can't convert mimetype %2 to extension", $vs_field, $vs_output_mimetype), "mediaproc->process()");
                 $o_media->cleanup();
                 return false;
             }
             if (($vs_dirhash = $this->_getDirectoryHash($va_volume_info["absolutePath"], $vn_id)) === false) {
                 $this->error->setError(1600, _t("Couldn't create subdirectory for file for %1", $vs_field), "mediaproc->process()");
                 $o_media->cleanup();
                 return false;
             }
             $vs_magic = rand(0, 99999);
             $vs_filepath = $va_volume_info["absolutePath"] . "/" . $vs_dirhash . "/" . $vs_magic . "_" . $vs_table . "_" . $vs_field . "_" . $vn_id . "_" . $v . "." . $vs_ext;
             if (!copy($vs_input_file, $vs_filepath)) {
                 $this->error->setError(504, _t("File could not be copied for %1", $vs_field), "mediaproc->process()");
                 $o_media->cleanup();
                 return false;
             }
             if (is_array($va_volume_info["mirrors"]) && sizeof($va_volume_info["mirrors"]) > 0) {
                 $entity_key = join("/", array($vs_table, $vs_field, $vn_id, $v));
                 $row_key = join("/", array($vs_table, $vn_id));
                 foreach ($va_volume_info["mirrors"] as $vs_mirror_code => $va_mirror_info) {
                     $vs_mirror_method = $va_mirror_info["method"];
                     $vs_queue = $vs_mirror_method . "mirror";
                     $tq = new TaskQueue();
                     if (!$tq->cancelPendingTasksForEntity($entity_key)) {
                         $this->error->setError(560, _t("Could not cancel pending tasks"), "mediaproc->process()");
                         $o_media->cleanup();
                         return false;
                     }
                     if ($tq->addTask($vs_queue, array("MIRROR" => $vs_mirror_code, "VOLUME" => $va_version_settings['VOLUME'], "FIELD" => $vs_field, "TABLE" => $vs_table, "VERSION" => $v, "FILES" => array(array("FILE_PATH" => $vs_filepath, "ABS_PATH" => $va_volume_info["absolutePath"], "HASH" => $vs_dirhash, "FILENAME" => $vs_magic . "_" . $vs_table . "_" . $vs_field . "_" . $vn_id . "_" . $v . "." . $vs_ext)), "MIRROR_INFO" => $va_mirror_info, "PK" => $vs_pk, "PK_VAL" => $vn_id), array("priority" => 100, "entity_key" => $entity_key, "row_key" => $row_key))) {
                         continue;
                     } else {
                         $this->error->setError(100, _t("Couldn't queue mirror using '%1' for version '%2' (handler '%3')", $vs_mirror_method, $v, $vs_queue), "mediaproc->process()");
                     }
                 }
             }
             $media_desc[$v] = array("VOLUME" => $va_version_settings['VOLUME'], "MIMETYPE" => $vs_output_mimetype, "WIDTH" => $o_media->get("width"), "HEIGHT" => $o_media->get("height"), "PROPERTIES" => $o_media->getProperties(), "FILENAME" => $vs_table . "_" . $vs_field . "_" . $vn_id . "_" . $v . "." . $vs_ext, "HASH" => $vs_dirhash, "MAGIC" => $vs_magic, "EXTENSION" => $vs_ext, "MD5" => md5_file($vs_filepath));
         } else {
             $o_media->set('version', $v);
             while (list($operation, $pa_parameters) = each($va_rules)) {
                 if ($operation === 'SET') {
                     foreach ($pa_parameters as $pp => $pv) {
                         if ($pp == 'format') {
                             $vs_output_mimetype = $pv;
                         } else {
                             $o_media->set($pp, $pv);
                         }
                     }
                 } else {
                     if (!$o_media->transform($operation, $pa_parameters)) {
                         $this->error = $o_media->errors[0];
                         $o_media->cleanup();
                         return false;
                     }
                 }
             }
             if (!$vs_output_mimetype) {
                 $vs_output_mimetype = $vs_input_mimetype;
             }
             if (!($vs_ext = $o_media->mimetype2extension($vs_output_mimetype))) {
                 $this->error->setError(1600, _t("File could not be processed for %1; can't convert mimetype %2 to extension", $vs_field, $vs_output_mimetype), "mediaproc->process()");
                 $o_media->cleanup();
                 return false;
             }
             if (($vs_dirhash = $this->_getDirectoryHash($va_volume_info["absolutePath"], $vn_id)) === false) {
                 $this->error->setError(1600, _t("Couldn't create subdirectory for file for %1", $vs_field), "mediaproc->process()");
                 $o_media->cleanup();
                 return false;
             }
             $vs_magic = rand(0, 99999);
             $vs_filepath = $va_volume_info["absolutePath"] . "/" . $vs_dirhash . "/" . $vs_magic . "_" . $vs_table . "_" . $vs_field . "_" . $vn_id . "_" . $v;
             if (!($vs_output_file = $o_media->write($vs_filepath, $vs_output_mimetype, $va_options))) {
                 $this->error = $o_media->errors[0];
                 $o_media->cleanup();
                 return false;
             } else {
                 if ($vs_output_file === __CA_MEDIA_VIDEO_DEFAULT_ICON__ || $vs_output_file === __CA_MEDIA_AUDIO_DEFAULT_ICON__ || $vs_output_file === __CA_MEDIA_DOCUMENT_DEFAULT_ICON__) {
                     $vs_use_icon = $vs_output_file;
                 } else {
                     $va_output_files[] = $vs_output_file;
                 }
             }
             if (is_array($va_volume_info["mirrors"]) && sizeof($va_volume_info["mirrors"]) > 0) {
                 $entity_key = join("/", array($vs_table, $vs_field, $vn_id, $v));
                 $row_key = join("/", array($vs_table, $vn_id));
                 foreach ($va_volume_info["mirrors"] as $vs_mirror_code => $va_mirror_info) {
                     $vs_mirror_method = $va_mirror_info["method"];
                     $vs_queue = $vs_mirror_method . "mirror";
                     $tq = new TaskQueue();
                     if (!$tq->cancelPendingTasksForEntity($entity_key)) {
                         $this->error->setError(560, _t("Could not cancel pending tasks"), "mediaproc->process()");
                         $o_media->cleanup();
                         return false;
                     }
                     if ($tq->addTask($vs_queue, array("MIRROR" => $vs_mirror_code, "VOLUME" => $va_version_settings['VOLUME'], "FIELD" => $vs_field, "TABLE" => $vs_table, "VERSION" => $v, "FILES" => array(array("FILE_PATH" => $vs_filepath, "ABS_PATH" => $va_volume_info["absolutePath"], "HASH" => $vs_dirhash, "FILENAME" => $vs_magic . "_" . $vs_table . "_" . $vs_field . "_" . $vn_id . "_" . $v . "." . $vs_ext)), "MIRROR_INFO" => $va_mirror_info, "PK" => $vs_pk, "PK_VAL" => $vn_id), array("priority" => 100, "entity_key" => $entity_key, "row_key" => $row_key))) {
                         continue;
                     } else {
                         $this->error->setError(100, _t("Couldn't queue mirror using '%1' for version '%2' (handler '%3')", $vs_mirror_method, $v, $vs_queue), "mediaproc->process()");
                     }
                 }
             }
             if ($vs_use_icon) {
                 $media_desc[$v] = array("MIMETYPE" => $vs_output_mimetype, "USE_ICON" => $vs_use_icon, "WIDTH" => $o_media->get("width"), "HEIGHT" => $o_media->get("height"));
             } else {
                 $media_desc[$v] = array("VOLUME" => $va_version_settings['VOLUME'], "MIMETYPE" => $vs_output_mimetype, "WIDTH" => $o_media->get("width"), "HEIGHT" => $o_media->get("height"), "PROPERTIES" => $o_media->getProperties(), "FILENAME" => $vs_table . "_" . $vs_field . "_" . $vn_id . "_" . $v . "." . $vs_ext, "HASH" => $vs_dirhash, "MAGIC" => $vs_magic, "EXTENSION" => $vs_ext, "MD5" => md5_file($vs_filepath . "." . $vs_ext));
             }
         }
         if (!$vb_dont_delete_original_media) {
             $vs_old_media_path = $t_instance->getMediaPath($vs_field, $v);
             if ($vs_old_media_path && $vs_filepath . "." . $vs_ext != $vs_old_media_path && $vs_input_file != vs_old_media_path) {
                 //@unlink($t_instance->getMediaPath($vs_field, $v));
                 $va_old_media_to_delete[] = $vs_old_media_path;
             }
         }
     }
     #
     # Update record
     #
     if ($t_instance->load($vn_id)) {
         if (method_exists($t_instance, "useBlobAsMediaField")) {
             // support for attributes - force field to be FT_MEDIA
             $t_instance->useBlobAsMediaField(true);
         }
         $md = $t_instance->get($vs_field);
         $va_merged_media_desc = is_array($md) ? $md : array();
         foreach ($media_desc as $vs_k => $va_v) {
             $va_merged_media_desc[$vs_k] = $va_v;
         }
         $t_instance->setMode(ACCESS_WRITE);
         $t_instance->setMediaInfo($vs_field, $va_merged_media_desc);
         $t_instance->update();
         if ($t_instance->numErrors()) {
             # get rid of files we just created
             foreach ($va_output_files as $vs_to_delete) {
                 @unlink($vs_to_delete);
             }
             $this->error->setError(560, _t("Could not update %1.%2: %3", $vs_table, $vs_field, join(", ", $t_instance->getErrors())), "mediaproc->process()");
             $o_media->cleanup();
             return false;
         }
         $va_report['notes'][] = _t("Processed file %1", $vs_input_file);
         // Generate preview frames for media that support that (Eg. video)
         // and add them as "multifiles" assuming the current model supports that (ca_object_representations does)
         $o_config = Configuration::load();
         if (((bool) $o_config->get('video_preview_generate_frames') || (bool) $o_config->get('document_preview_generate_pages')) && method_exists($t_instance, 'addFile')) {
             $o_media->read($vs_input_file);
             $va_preview_frame_list = $o_media->writePreviews(array('width' => $o_media->get("width"), 'height' => $o_media->get("height"), 'numberOfFrames' => $o_config->get('video_preview_max_number_of_frames'), 'numberOfPages' => $o_config->get('document_preview_max_number_of_pages'), 'frameInterval' => $o_config->get('video_preview_interval_between_frames'), 'pageInterval' => $o_config->get('document_preview_interval_between_pages'), 'startAtTime' => $o_config->get('video_preview_start_at'), 'endAtTime' => $o_config->get('video_preview_end_at'), 'startAtPage' => $o_config->get('document_preview_start_page'), 'outputDirectory' => __CA_APP_DIR__ . '/tmp'));
             $t_instance->removeAllFiles();
             // get rid of any previously existing frames (they might be hanging around if we're editing an existing record)
             if (is_array($va_preview_frame_list)) {
                 foreach ($va_preview_frame_list as $vn_time => $vs_frame) {
                     $t_instance->addFile($vs_frame, $vn_time, true);
                     // the resource path for each frame is it's time, in seconds (may be fractional) for video, or page number for documents
                     @unlink($vs_frame);
                     // clean up tmp preview frame file
                 }
             }
         }
         if (!$vb_dont_delete_original_media) {
             @unlink($vs_input_file);
         }
         foreach ($va_old_media_to_delete as $vs_to_delete) {
             @unlink($vs_to_delete);
         }
         $o_media->cleanup();
         return $va_report;
     } else {
         # record no longer exists
         if (!$vb_dont_delete_original_media) {
             @unlink($vs_input_file);
         }
         $o_eventlog = new EventLog();
         $o_eventlog->log(array("CODE" => "DEBG", "SOURCE" => "TaskQueue->mediaproc->process()", "MESSAGE" => "Record {$vs_table}.field = {$vn_id} did not exist; queued file was discarded"));
         $o_media->cleanup();
         $va_report['errors'][] = _t("Record {$vs_table}.field = {$vn_id} did not exist; queued file was discarded");
         return $va_report;
     }
 }
Example #23
0
 /**
  * 
  *
  * @param string $ps_field
  * @param string $ps_target
  * @return bool
  */
 public function replicateMedia($ps_field, $ps_target)
 {
     global $AUTH_CURRENT_USER_ID;
     $va_targets = $this->getMediaReplicationTargets($ps_field, 'original');
     $va_target_info = $va_targets[$ps_target];
     // TODO: generalize this metadata? Perhaps let the plugin deal with it?
     $pa_data = array('title' => $va_target_info['options']['title'] ? caProcessTemplateForIDs($va_target_info['options']['title'], $this->tableNum(), array($this->getPrimaryKey()), array('requireLinkTags' => true)) : "", 'description' => $va_target_info['options']['description'] ? caProcessTemplateForIDs($va_target_info['options']['description'], $this->tableNum(), array($this->getPrimaryKey()), array('requireLinkTags' => true)) : "", 'tags' => $va_target_info['options']['tags'] ? caProcessTemplateForIDs($va_target_info['options']['tags'], $this->tableNum(), array($this->getPrimaryKey()), array('requireLinkTags' => true)) : "", 'category' => $va_target_info['options']['category']);
     $vs_version = $va_target_info['version'];
     if (!in_array($vs_version, $this->getMediaVersions($ps_field))) {
         $vs_version = 'original';
     }
     $o_replicator = new MediaReplicator();
     if ($this->getAppConfig()->get('queue_enabled')) {
         // Queue replication
         $o_tq = new TaskQueue();
         $vs_row_key = join("/", array($this->tableName(), $this->getPrimaryKey()));
         $vs_entity_key = join("/", array($this->tableName(), $ps_field, $this->getPrimaryKey(), $vs_version));
         if (!$o_tq->cancelPendingTasksForEntity($vs_entity_key)) {
             //$this->postError(560, _t("Could not cancel pending tasks: %1", $this->error),"BaseModel->replicateMedia()");
             //return false;
         }
         if ($o_tq->addTask('mediaReplication', array("FIELD" => $ps_field, "TARGET" => $ps_target, "TABLE" => $this->tableName(), "VERSION" => $vs_version, "TARGET_INFO" => $va_target_info, "DATA" => $pa_data, "PK" => $this->primaryKey(), "PK_VAL" => $this->getPrimaryKey()), array("priority" => 100, "entity_key" => $vs_entity_key, "row_key" => $vs_row_key, 'user_id' => $AUTH_CURRENT_USER_ID))) {
             $va_media_info = $this->getMediaInfo($ps_field);
             $va_media_info['REPLICATION_STATUS'][$ps_target] = __CA_MEDIA_REPLICATION_STATE_PENDING__;
             // pending means it's queued
             $va_media_info['REPLICATION_LOG'][$ps_target][] = array('STATUS' => __CA_MEDIA_REPLICATION_STATE_PENDING__, 'DATETIME' => time());
             $va_media_info['REPLICATION_KEYS'][$ps_target] = null;
             $this->setMediaInfo($ps_field, $va_media_info);
             $this->setMode(ACCESS_WRITE);
             $this->update();
             return null;
         } else {
             $this->postError(100, _t("Couldn't queue mirror using '%1' for version '%2'", 'mediaReplication', $vs_version), "BaseModel->replicateMedia()");
         }
         $vs_key = null;
     } else {
         // Do replication right now, in-process
         // (mark replication as started)
         $va_media_info = $this->getMediaInfo($ps_field);
         $va_media_info['REPLICATION_STATUS'][$ps_target] = __CA_MEDIA_REPLICATION_STATE_UPLOADING__;
         $va_media_info['REPLICATION_LOG'][$ps_target][] = array('STATUS' => __CA_MEDIA_REPLICATION_STATE_UPLOADING__, 'DATETIME' => time());
         $va_media_info['REPLICATION_KEYS'][$ps_target] = null;
         $this->setMediaInfo($ps_field, $va_media_info);
         $this->setMode(ACCESS_WRITE);
         $this->update(array('processingMediaForReplication' => true));
         if (!is_array($va_media_desc = $this->_FIELD_VALUES[$ps_field])) {
             throw new Exception(_t("Could not record replication status: %1 has no media description data", $ps_field));
         }
         try {
             if ($vs_key = $o_replicator->replicateMedia($this->getMediaPath($ps_field, $vs_version), $va_target_info, $pa_data)) {
                 $vn_status = $o_replicator->getReplicationStatus($va_target_info, $vs_key);
                 $va_media_info['REPLICATION_STATUS'][$ps_target] = $vn_status;
                 $va_media_info['REPLICATION_LOG'][$ps_target][] = array('STATUS' => $vn_status, 'DATETIME' => time());
                 $va_media_info['REPLICATION_KEYS'][$ps_target] = $vs_key;
                 $this->setMediaInfo($ps_field, $va_media_info);
                 $this->update(array('processingMediaForReplication' => true));
             } else {
                 $va_media_info['REPLICATION_STATUS'][$ps_target] = __CA_MEDIA_REPLICATION_STATE_ERROR__;
                 $va_media_info['REPLICATION_LOG'][$ps_target][] = array('STATUS' => __CA_MEDIA_REPLICATION_STATE_ERROR__, 'DATETIME' => time());
                 $va_media_info['REPLICATION_KEYS'][$ps_target] = null;
                 $this->setMediaInfo($ps_field, $va_media_info);
                 $this->update(array('processingMediaForReplication' => true));
                 $this->postError(3300, _t('Media replication for %1 failed', $va_target_info['name']), 'BaseModel->replicateMedia', $this->tableName() . '.' . $ps_field);
                 return null;
             }
         } catch (Exception $e) {
             $va_media_info['REPLICATION_STATUS'][$ps_target] = __CA_MEDIA_REPLICATION_STATE_ERROR__;
             $va_media_info['REPLICATION_LOG'][$ps_target][] = array('STATUS' => __CA_MEDIA_REPLICATION_STATE_ERROR__, 'DATETIME' => time());
             $va_media_info['REPLICATION_KEYS'][$ps_target] = null;
             $this->setMediaInfo($ps_field, $va_media_info);
             $this->update(array('processingMediaForReplication' => true));
             $this->postError(3300, _t('Media replication for %1 failed: %2', $va_target_info['name'], $e->getMessage()), 'BaseModel->replicateMedia', $this->tableName() . '.' . $ps_field);
             return null;
         }
     }
     return $vs_key;
 }
 /**
  * Saves the content of a form editing new or existing records. It returns the same form + status messages rendered into the current view, inherited from ActionController
  *
  * @param array $pa_options Array of options passed through to _initView and saveBundlesForScreen()
  */
 public function Save($pa_options = null)
 {
     global $g_ui_locale_id;
     if (!is_array($pa_options)) {
         $pa_options = array();
     }
     list($t_ui) = $this->_initView($pa_options);
     $vs_import_target = $this->getRequest()->getParameter('import_target', pString);
     if (!$this->getRequest()->getAppDatamodel()->tableExists($vs_import_target)) {
         $vs_import_target = 'ca_objects';
     }
     $vs_directory = $this->request->getParameter('directory', pString);
     $vs_batch_media_import_root_directory = $this->request->config->get('batch_media_import_root_directory');
     if (preg_match("!/\\.\\.!", $vs_directory) || preg_match("!\\.\\./!", $vs_directory)) {
         $this->response->setRedirect($this->request->config->get('error_display_url') . '/n/3250?r=' . urlencode($this->request->getFullUrlPath()));
         return;
     }
     if (!is_dir($vs_batch_media_import_root_directory . '/' . $vs_directory)) {
         $this->response->setRedirect($this->request->config->get('error_display_url') . '/n/3250?r=' . urlencode($this->request->getFullUrlPath()));
         return;
     }
     $va_options = array('sendMail' => (bool) $this->request->getParameter('send_email_when_done', pInteger), 'sendSMS' => (bool) $this->request->getParameter('send_sms_when_done', pInteger), 'runInBackground' => (bool) $this->request->getParameter('run_in_background', pInteger), 'importFromDirectory' => $vs_batch_media_import_root_directory . '/' . $vs_directory, 'includeSubDirectories' => (bool) $this->request->getParameter('include_subdirectories', pInteger), 'deleteMediaOnImport' => (bool) $this->request->getParameter('delete_media_on_import', pInteger), 'importMode' => $this->request->getParameter('import_mode', pString), 'matchMode' => $this->request->getParameter('match_mode', pString), 'matchType' => $this->request->getParameter('match_type', pString), $vs_import_target . '_limit_matching_to_type_ids' => $this->request->getParameter($vs_import_target . '_limit_matching_to_type_ids', pArray), $vs_import_target . '_type_id' => $this->request->getParameter($vs_import_target . '_type_id', pInteger), 'ca_object_representations_type_id' => $this->request->getParameter('ca_object_representations_type_id', pInteger), $vs_import_target . '_status' => $this->request->getParameter($vs_import_target . '_status', pInteger), 'ca_object_representations_status' => $this->request->getParameter('ca_object_representations_status', pInteger), $vs_import_target . '_access' => $this->request->getParameter($vs_import_target . '_access', pInteger), 'ca_object_representations_access' => $this->request->getParameter('ca_object_representations_access', pInteger), $vs_import_target . '_mapping_id' => $this->request->getParameter($vs_import_target . '_mapping_id', pInteger), 'ca_object_representations_mapping_id' => $this->request->getParameter('ca_object_representations_mapping_id', pInteger), 'setMode' => $this->request->getParameter('set_mode', pString), 'setCreateName' => $this->request->getParameter('set_create_name', pString), 'set_id' => $this->request->getParameter('set_id', pInteger), 'idnoMode' => $this->request->getParameter('idno_mode', pString), 'idno' => $this->request->getParameter('idno', pString), 'representationIdnoMode' => $this->request->getParameter('representation_idno_mode', pString), 'representation_idno' => $this->request->getParameter('idno_representation_number', pString), 'logLevel' => $this->request->getParameter('log_level', pString), 'allowDuplicateMedia' => $this->request->getParameter('allow_duplicate_media', pInteger), 'locale_id' => $g_ui_locale_id, 'user_id' => $this->request->getUserID(), 'skipFileList' => $this->request->getParameter('skip_file_list', pString), 'importTarget' => $vs_import_target);
     if ($vn_rel_type = $this->request->getParameter($vs_import_target . '_representation_relationship_type', pInteger)) {
         $va_options[$vs_import_target . '_representation_relationship_type'] = $vn_rel_type;
     }
     if (is_array($va_create_relationships_for = $this->request->getParameter('create_relationship_for', pArray))) {
         $va_options['create_relationship_for'] = $va_create_relationships_for;
         foreach ($va_create_relationships_for as $vs_rel_table) {
             $va_options['relationship_type_id_for_' . $vs_rel_table] = $this->request->getParameter('relationship_type_id_for_' . $vs_rel_table, pString);
         }
     }
     $va_last_settings = $va_options;
     $va_last_settings['importFromDirectory'] = preg_replace("!{$vs_batch_media_import_root_directory}[/]*!", "", $va_last_settings['importFromDirectory']);
     $this->request->user->setVar('batch_mediaimport_last_settings', $va_last_settings);
     if ((bool) $this->request->config->get('queue_enabled') && (bool) $this->request->getParameter('run_in_background', pInteger)) {
         // queue for background processing
         $o_tq = new TaskQueue();
         $vs_row_key = $vs_entity_key = join("/", array($this->request->getUserID(), $va_options['importFromDirectory'], time(), rand(1, 999999)));
         if (!$o_tq->addTask('mediaImport', $va_options, array("priority" => 100, "entity_key" => $vs_entity_key, "row_key" => $vs_row_key, 'user_id' => $this->request->getUserID()))) {
             //$this->postError(100, _t("Couldn't queue batch processing for"),"EditorContro->_processMedia()");
         }
         $this->render('mediaimport/batch_queued_html.php');
     } else {
         // run now
         $app = AppController::getInstance();
         $app->registerPlugin(new BatchMediaImportProgress($this->request, $va_options));
         $this->render('mediaimport/batch_results_html.php');
     }
 }