/** * Starts the process of adding an email to a saved user object. * Also modifies and saves this user object back to the database. * @method addEmail * @param {string} $emailAddress * The email address to add. * @param {string} [$activationEmailSubject=null] * The subject of the activation email to send. * @param {string} [$activationEmailView=null] * The view to use for the body of the activation email to send. * @param {boolean} [$html=true] * Defaults to true. Whether to send as HTML email. * @param {array} [$fields=array()] * An array of additional fields to pass to the email view. * @param {array} $options=array() * Array of options. Can include:<br/> * "html" => Defaults to false. Whether to send as HTML email.<br/> * "name" => A human-readable name in addition to the address.<br/> * "from" => An array of (emailAddress, human_readable_name)<br/> * "delay" => A delay, in milliseconds, to wait until sending email. Only works if Node server is listening. * @return {boolean} * Returns true on success. * Returns false if this email address is already verified for this user. * @throws {Q_Exception_WrongValue} * If the email address is in an invalid format, this is thrown. * @throws {Users_Exception_AlreadyVerified} * If the email address already exists and has been verified for * another user, then this exception is thrown. */ function addEmail($emailAddress, $activationEmailSubject = null, $activationEmailView = null, $fields = array(), $options = array()) { if (!isset($options['html'])) { $options['html'] = true; } if (!Q_Valid::email($emailAddress, $normalized)) { throw new Q_Exception_WrongValue(array('field' => 'Email', 'range' => 'a valid address'), 'emailAddress'); } $email = new Users_Email(); $email->address = $normalized; if ($email->retrieve('*', array('ignoreCache' => true)) and $email->state !== 'unverified') { if ($email->userId === $this->id) { $email->set('user', $this); return $email; } // Otherwise, say it's verified for another user, // even if it unsubscribed or was suspended. throw new Users_Exception_AlreadyVerified(array('key' => 'email address', 'userId' => $email->userId), 'emailAddress'); } $user = $this; // If we are here, then the email record either // doesn't exist, or hasn't been verified yet. // In either event, update the record in the database, // and re-send the email. $minutes = Q_Config::get('Users', 'activation', 'expires', 60 * 24 * 7); $email->state = 'unverified'; $email->userId = $this->id; $email->activationCode = strtolower(Q_Utils::unique(7)); $email->activationCodeExpires = new Db_Expression("CURRENT_TIMESTAMP + INTERVAL {$minutes} MINUTE"); $email->authCode = md5(microtime() + mt_rand()); $link = 'Users/activate?code=' . urlencode($email->activationCode) . ' emailAddress=' . urlencode($email->address); /** * @event Users/addIdentifier {before} * @param {string} user * @param {string} email */ Q::event('Users/addIdentifier', compact('user', 'email', 'link'), 'before'); $email->save(); $this->emailAddressPending = $normalized; $this->save(); if (!isset($activationEmailView)) { $activationEmailView = Q_Config::get('Users', 'transactional', 'activation', 'body', 'Users/email/activation.php'); } if (!isset($activationEmailSubject)) { $activationEmailSubject = Q_Config::get('Users', 'transactional', 'activation', 'subject', "Welcome! Please confirm your email address."); } $fields2 = array_merge($fields, array('user' => $this, 'email' => $email, 'app' => Q_Config::expect('Q', 'app'), 'baseUrl' => Q_Request::baseUrl(), 'link' => $link)); $email->sendMessage($activationEmailSubject, $activationEmailView, $fields2, $options); // may throw exception if badly configured /** * @event Users/addIdentifier {after} * @param {string} user * @param {string} email */ Q::event('Users/addIdentifier', compact('user', 'email', 'link'), 'after'); }
/** * Starts the process of adding an email to a saved user object. * Also modifies and saves this user object back to the database. * @param string $email_address * The email address to add. * @param string $activation_email_subject * The subject of the activation email to send. * @param string $activation_email_view * The view to use for the body of the activation email to send. * @param boolean $html * Defaults to true. Whether to send as HTML email. * @param array $fields * An array of additional fields to pass to the email view. * @return boolean * Returns true on success. * Returns false if this email address is already verified for this user. * @throws Pie_Exception_WrongType * If the email address is in an invalid format, this is thrown. * @throws Users_Exception_AlreadyVerified * If the email address already exists and has been verified for * another user, then this exception is thrown. */ function addEmail($email_address, $activation_email_subject = null, $activation_email_view = null, $html = true, $fields = array()) { if (!Pie_Valid::email($email_address)) { throw new Pie_Exception_WrongValue(array('field' => 'Email', 'range' => 'a valid address'), 'email_address'); } Pie::event('users/validate/email_address', array('email_address' => &$email_address)); $e = new Users_Email(); $e->address = $email_address; if ($e->retrieve() and $e->state !== 'unverified') { if ($e->user_id === $this->id) { return false; } // Otherwise, say it's verified for another user, // even if it unsubscribed or was suspended. throw new Users_Exception_AlreadyVerified(array('key' => $e->address, 'user_id' => $e->user_id), 'email_address'); } // If we are here, then the email record either // doesn't exist, or hasn't been verified yet. // In either event, update the record in the database, // and re-send the email. $minutes = Pie_Config::get('users', 'activationCodeExpires', 60 * 24 * 7); $e->state = 'unverified'; $e->user_id = $this->id; $e->activation_code = Pie_Utils::unique(5); $e->activation_code_expires = new Db_Expression("CURRENT_TIMESTAMP + INTERVAL {$minutes} MINUTE"); $e->auth_code = md5(microtime() + mt_rand()); $e->save(); if (!isset($activation_email_view)) { $activation_email_view = Pie_Config::get('users', 'activationEmailView', 'users/email/activation.php'); } if (!isset($activation_email_subject)) { $activation_email_subject = Pie_Config::get('users', 'activationEmailSubject', "Welcome! Please confirm your email address."); } $fields2 = array_merge($fields, array('user' => $this, 'email' => $e)); $e->sendMessage($activation_email_subject, $activation_email_view, $fields2, array('html' => $html)); Pie::event('users/addEmail', compact('email_address'), 'after'); }
function Shipping_scheduled_post() { $env = Shipping::getVars(); if (empty($_REQUEST["streamName"])) { throw new Q_Exception_WrongValue(array('field' => 'streamName', 'range' => "not empty")); } if (empty($_REQUEST["to"]["street"])) { throw new Q_Exception_WrongValue(array('field' => 'destination address street', 'range' => "not empty")); } if (empty($_REQUEST["to"]["city"])) { throw new Q_Exception_WrongValue(array('field' => 'destination address city', 'range' => "not empty")); } if (empty($_REQUEST["to"]["zipcode"])) { throw new Q_Exception_WrongValue(array('field' => 'destination address zipcode', 'range' => "not empty")); } if (empty($_REQUEST["to"]["country"])) { throw new Q_Exception_WrongValue(array('field' => 'destination address country', 'range' => "not empty")); } //if(empty($_REQUEST["to"]["state"])) throw new Q_Exception_WrongValue(array('field' => 'destination address state', 'range' => "not empty")); if (empty($_REQUEST["from"]["street"])) { throw new Q_Exception_WrongValue(array('field' => 'origin address street', 'range' => "not empty")); } if (empty($_REQUEST["from"]["city"])) { throw new Q_Exception_WrongValue(array('field' => 'origin address city', 'range' => "not empty")); } if (empty($_REQUEST["from"]["zipcode"])) { throw new Q_Exception_WrongValue(array('field' => 'origin address zipcode', 'range' => "not empty")); } if (empty($_REQUEST["from"]["country"])) { throw new Q_Exception_WrongValue(array('field' => 'origin address country', 'range' => "not empty")); } //if(empty($_REQUEST["from"]["state"])) throw new Q_Exception_WrongValue(array('field' => 'original address state', 'range' => "not empty")); if (empty($_REQUEST['packages'])) { throw new Q_Exception_WrongValue(array('field' => 'packages', 'range' => "not empty")); } if (empty($_REQUEST["carrier"]) || !count($_REQUEST["carrier"])) { throw new Q_Exception_WrongValue(array('field' => 'carrier', 'range' => "not empty")); } if (empty($_REQUEST["dateScheduled"])) { throw new Q_Exception_WrongValue(array('field' => 'dateScheduled', 'range' => "not empty")); } if (empty($_REQUEST["asUser"])) { $_REQUEST["asUser"] = $env->userId; } $user = Users::loggedInUser(true); //print_r($user); exit; $email = new Users_Email(); $email->address = $user->emailAddress ?: $user->emailAddressPending; $email->userId = $user->id; //$email->save(); //$shipmentStream = Shipping::shipment(); $shipmentStream = Streams::fetchOne($env->communityId, $env->communityId, $_REQUEST["streamName"]); //$shipmentStream->clearAllAttributes(); $shipmentStream->title = Q::ifset($_REQUEST, 'title', "(empty)"); $shipmentStream->userId = $env->userId; $shipmentStream->destination = json_encode($_REQUEST["to"]); $shipmentStream->origin = json_encode($_REQUEST["from"]); $shipmentStream->receiver = json_encode(Q::ifset($_REQUEST, 'receiver', new StdClass())); $shipmentStream->dateScheduled = Q::ifset($_REQUEST, 'dateScheduled', ""); // set packages $packages = Q::ifset($_REQUEST, 'packages', new StdClass()); if (!is_object($packages)) { $packages["totalWeight"] = 0; $packages["totalVolume"] = 0; $packages["totalAmount"] = 0; foreach ($packages["packages"] as $key => $package) { if (!isset($package['weight'])) { throw new Q_Exception("One of the packages doesn't have its weight set"); } $packages["totalWeight"] += $package["weight"]; $volume = round((double) $package["width"] * (double) $package["height"] * (double) $package["length"], 3); $packages["totalVolume"] += $volume; $packages["packages"][$key]["volume"] = $volume; //$totalValue = round((float)$package["value"] * (int)$package["quantity"], 2); //$packages["packages"][$key]["totalValue"] = $totalValue; //$packages["totalInvoiceValue"] += $totalValue; $packages["totalAmount"]++; } } // set products $products = Q::ifset($_REQUEST, 'products', new StdClass()); if (!is_object($products)) { $products["totalWeight"] = 0; $products["totalAmount"] = 0; $products["totalInvoiceValue"] = 0; foreach ($products["packages"] as $key => $product) { // $products["totalWeight"] += $product["weight"]; //$volume = round($product["w"] * $product["h"] * $product["l"], 3); //$packages["totalVolume"] += $volume; //$products["packages"][$key]["volume"] = $volume; $totalValue = round((double) $product["value"] * (int) $product["quantity"], 2); $products["packages"][$key]["totalValue"] = $totalValue; $products["totalInvoiceValue"] += $totalValue; $products["totalAmount"]++; } } $shipmentStream->packages = json_encode($packages); $shipmentStream->products = json_encode($products); $shipmentStream->carrier = json_encode(Shipping_Carrier::createCarrier($shipmentStream, $_REQUEST["carrier"])); $shipmentStream->invoiceOptions = json_encode(Q::ifset($_REQUEST, 'invoice', new StdClass())); $shipmentStream->collectInstructions = json_encode(Q::ifset($_REQUEST, 'collect', new StdClass())); // set template attributes $isTemplate = (bool) $_REQUEST["isTemplate"]; if ($isTemplate) { $shipmentStream->setAttribute("isTemplate", true); $shipmentStream->setAttribute("templateName", $_REQUEST["template"]["templateName"]); } else { $shipmentStream->clearAttribute("isTemplate"); $shipmentStream->clearAttribute("templateName"); } // set asUser attribute $shipmentStream->setAttribute("asUser", $_REQUEST["asUser"]); $shipmentStream->setAttribute("fromUser", $env->userId); $shipmentStream->save(); $isPickupLater = (bool) $_REQUEST["isPickupLater"]; //throw new exception("Test!"); // return if action = invoice preview if ($_REQUEST["isPreview"]) { return; } // relate to category stream "Shipping/templates" if ($isTemplate) { Shipping::shipmentToTemplate($shipmentStream); } // create carrier object if (empty($_REQUEST["carrier"]["name"])) { throw new Q_Exception_WrongValue(array('field' => 'carrier name', 'range' => "not empty")); } $carrierClassName = "Shipping_Carrier_" . $_REQUEST["carrier"]["name"]; if (!class_exists($carrierClassName)) { throw new exception("carrier class " . $carrierClassName . " don't exist!"); } $carrier = new $carrierClassName(); // trying to send shipment $carrier->ship($shipmentStream, $isPickupLater); // send shipment confirmation to user if ($email->address) { $email->sendMessage(Q_Config::get('Users', 'transactional', 'scheduled', 'subject', false), Q_Config::get('Users', 'transactional', 'scheduled', 'body', false), array('user' => $user, 'shipment' => $shipmentStream), array('html' => false)); // may throw exception if badly configured } //Q_Request::requireFields(array('streamName'), $_REQUEST, true); //$params = Q::take($_REQUEST, array("streamName")); //$stream = Streams::fetchOne($userId, $userId, $params["streamName"]); }
function Assets_after_Assets_charge($params) { $user = $payments = $amount = $currency = $charge = $adapter = $options = null; extract($params, EXTR_OVERWRITE); $description = 'a product or service'; $stream = Q::ifset($options, 'stream', null); if ($stream) { $publisherId = $stream->publisherId; $publisher = Users_User::fetch($publisherId, true); if ($stream->type === 'Assets/subscription') { $plan = Streams::fetchOne($stream->getAttribute('planPublisherId'), $stream->getAttribute('planPublisherId'), $stream->getAttribute('planStreamName'), true); $months = $stream->getAttribute('months'); $startDate = $stream->getAttribute('startDate'); $endDate = $stream->getAttribute('endDate'); } $description = $stream->title; } else { $publisherId = Users::communityId(); $publisher = Users_User::fetch($publisherId, true); } if (isset($options['description'])) { $description = $options['description']; } $currencies = Q::json_decode(file_get_contents(ASSETS_PLUGIN_CONFIG_DIR . DS . 'currencies.json'), true); if (!isset($currencies['symbols'][$currency])) { throw new Q_Exception_BadValue(array('internal' => 'currency', 'problem' => 'no symbol found'), 'currency'); } if (!isset($currencies['names'][$currency])) { throw new Q_Exception_BadValue(array('internal' => 'currency', 'problem' => 'no name found'), 'currency'); } $symbol = $currencies['symbols'][$currency]; $currencyName = $currencies['names'][$currency]; $communityId = Users::communityId(); $communityName = Users::communityName(); $communitySuffix = Users::communitySuffix(); $link = Q_Request::baseUrl('action.php') . "/Assets/payment?publisherId={$publisherId}&userId=" . $user->id; $fields = compact('user', 'publisher', 'publisherId', 'communityId', 'communityName', 'communitySuffix', 'description', 'subscription', 'stream', 'plan', 'currency', 'name', 'symbol', 'currencyName', 'amount', 'months', 'startDate', 'endDate', 'link'); if ($user->emailAddress) { $email = new Users_Email(); $email->address = $user->emailAddress; $email->retrieve(true); $emailSubject = Q_Config::get('Assets', 'transactional', 'charged', 'subject', false); $emailView = Q_Config::get('Assets', 'transactional', 'charged', 'body', false); if ($emailSubject !== false and $emailView) { $email->sendMessage($emailSubject, $emailView, $fields); } } else { if ($user->mobileNumber) { $mobile = new Users_Mobile(); $mobile->number = $user->mobileNumber; $mobile->retrieve(true); if ($mobileView = Q_Config::get('Assets', 'transactional', 'charged', 'sms', false)) { $mobile->sendMessage($mobileView, $fields); } } } if ($publisher->emailAddress) { $email = new Users_Email(); $email->address = $publisher->emailAddress; $email->retrieve(true); $emailSubject = Q_Config::get('Assets', 'transactional', 'charge', 'subject', false); $emailView = Q_Config::get('Assets', 'transactional', 'charge', 'body', false); if ($emailSubject !== false and $emailView) { $email->sendMessage($emailSubject, $emailView, $fields); } } else { if ($publisher->mobileNumber) { $mobile = new Users_Mobile(); $mobile->number = $publisher->mobileNumber; $mobile->retrieve(true); if ($mobileView = Q_Config::get('Assets', 'transactional', 'charge', 'sms', false)) { $mobile->sendMessage($mobileView, $fields); } } } }