/** * Stores a value in a cookie, by default the cookie will expire in one day. * * @param string $key A name for the cookie. * @param mixed $value The value to be stored. Keep in mind that they will be serialized. * @param int[optional] $time The number of seconds that this cookie will be available, 30 days is the default. * @param string[optional] $path The path on the server in which the cookie will be availabe. Use / for the entire domain, /foo if you just want it to be available in /foo. * @param string[optional] $domain The domain that the cookie is available on. Use .example.com to make it available on all subdomains of example.com. * @param bool[optional] $secure Should the cookie be transmitted over a HTTPS-connection? If true, make sure you use a secure connection, otherwise the cookie won't be set. * @param bool[optional] $httpOnly Should the cookie only be available through HTTP-protocol? If true, the cookie can't be accessed by Javascript, ... * @return bool If set with succes, returns true otherwise false. */ public static function set($key, $value, $time = 2592000, $path = '/', $domain = null, $secure = null, $httpOnly = true) { // redefine $key = (string) $key; $value = serialize($value); $time = time() + (int) $time; $path = (string) $path; $httpOnly = (bool) $httpOnly; // when the domain isn't passed and the url-object is available we can set the cookies for all subdomains if ($domain === null && Spoon::exists('url')) { $domain = '.' . Spoon::get('url')->getDomain(); } // when the secure-parameter isn't set if ($secure === null) { /* detect if we are using HTTPS, this wil only work in Apache, if you are using nginx you should add the code below into your config: ssl on; fastcgi_param HTTPS on; for lighttpd you should add: setenv.add-environment = ("HTTPS" => "on") */ $secure = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on'; } // set cookie $cookie = setcookie($key, $value, $time, $path, $domain, $secure, $httpOnly); // problem occured return $cookie === false ? false : true; }
/** * Returns the CampaignMonitor object * * @param int[optional] $listId The default list id to use. * @return CampaignMonitor */ public static function getCM($listId = null) { // campaignmonitor reference exists if (!Spoon::exists('campaignmonitor')) { // check if the CampaignMonitor class exists if (!SpoonFile::exists(PATH_LIBRARY . '/external/campaignmonitor.php')) { // the class doesn't exist, so throw an exception throw new SpoonFileException('The CampaignMonitor wrapper class is not found. Please locate and place it in /library/external'); } // require CampaignMonitor class require_once 'external/campaignmonitor.php'; // set login data $url = FrontendModel::getModuleSetting('mailmotor', 'cm_url'); $username = FrontendModel::getModuleSetting('mailmotor', 'cm_username'); $password = FrontendModel::getModuleSetting('mailmotor', 'cm_password'); // init CampaignMonitor object $cm = new CampaignMonitor($url, $username, $password, 5, self::getClientId()); // set CampaignMonitor object reference Spoon::set('campaignmonitor', $cm); // get the default list ID $listId = !empty($listId) ? $listId : self::getDefaultListID(); // set the default list ID $cm->setListId($listId); } // return the CampaignMonitor object return Spoon::get('campaignmonitor'); }
public function testSet() { // set value $value = array('Davy Hellemans', 'Tijs Verkoyen', 'Dave Lens', 'Matthias Mullie'); $this->assertEquals(Spoon::set('salad_fingers', $value), $value); // get rid of value Spoon::set('salad_fingers'); $this->assertFalse(Spoon::exists('salad_fingers')); }
/** * Check if all required settings have been set * * @param string $module The module. */ public function __construct($module) { parent::__construct($module); $this->loadEngineFiles(); $url = Spoon::exists('url') ? Spoon::get('url') : null; // do the client ID check if we're not in the settings page if ($url != null && !in_array($url->getAction(), array('settings', 'import_groups', 'link_account', 'load_client_info'))) { $this->checkForAccount(); $this->checkForClientID(); $this->checkForGroups(); } }
/** * Check if all required settings have been set * * @return void * @param string $module The module. */ public function __construct($module) { // parent construct parent::__construct($module); // load additional engine files $this->loadEngineFiles(); // get url object reference $url = Spoon::exists('url') ? Spoon::get('url') : null; // do the client ID check if we're not in the settings page if ($url != null && $url->getAction() != 'settings' && $url->getAction() != 'import_groups' && strpos($url->getQueryString(), 'link_account') === false && strpos($url->getQueryString(), 'load_client_info') === false) { // check for CM account $this->checkForAccount(); // check for client ID $this->checkForClientID(); // check for groups $this->checkForGroups(); } }
/** * Check if all required settings have been set * * @param string $module The module. */ public function __construct($module) { parent::__construct($module); $error = false; $action = Spoon::exists('url') ? Spoon::get('url')->getAction() : null; // analytics session token if (BackendModel::getModuleSetting('analytics', 'session_token') === null) { $error = true; } // analytics table id if (BackendModel::getModuleSetting('analytics', 'table_id') === null) { $error = true; } // missing settings, so redirect to the index-page to show a message (except on the index- and settings-page) if ($error && $action != 'settings' && $action != 'index') { SpoonHTTP::redirect(BackendModel::createURLForAction('index')); } }
/** * @param string[optional] $name Name of the form. * @param string[optional] $action The action (URL) whereto the form will be submitted, if not provided it will be autogenerated. * @param string[optional] $method The method to use when submiting the form, default is POST. * @param bool[optional] $useToken Should we automagically add a formtoken? * @param bool[optional] $useGlobalError Should we automagically show a global error? */ public function __construct($name = null, $action = null, $method = 'post', $useToken = true, $useGlobalError = true) { if (Spoon::exists('url')) { $this->URL = Spoon::get('url'); } if (Spoon::exists('header')) { $this->header = Spoon::get('header'); } $this->useGlobalError = (bool) $useGlobalError; // build a name if there wasn't one provided $name = $name === null ? SpoonFilter::toCamelCase($this->URL->getModule() . '_' . $this->URL->getAction(), '_', true) : (string) $name; // build the action if it wasn't provided $action = $action === null ? '/' . $this->URL->getQueryString() : (string) $action; // call the real form-class parent::__construct($name, $action, $method, $useToken); // add default classes $this->setParameter('id', $name); $this->setParameter('class', 'forkForms submitWithLink'); }
/** * The constructor will store the instance in the reference, preset some settings and map the custom modifiers. * * @param bool[optional] $addToReference Should the instance be added into the reference. */ public function __construct($addToReference = true) { parent::__construct(); // get URL instance if (Spoon::exists('url')) { $this->URL = Spoon::get('url'); } // store in reference so we can access it from everywhere if ($addToReference) { Spoon::set('template', $this); } // set cache directory $this->setCacheDirectory(BACKEND_CACHE_PATH . '/cached_templates'); // set compile directory $this->setCompileDirectory(BACKEND_CACHE_PATH . '/compiled_templates'); // when debugging, the template should be recompiled every time $this->setForceCompile(SPOON_DEBUG); // map custom modifiers $this->mapCustomModifiers(); }
/** * Default constructor * * @return void * @param BackendForm $form An instance of Backendform, the elements will be parsed in here. * @param int[optional] $metaId The metaID to load. * @param string[optional] $baseFieldName The field where the URL should be based on. * @param bool[optional] $custom Add/show custom-meta. */ public function __construct(BackendForm $form, $metaId = null, $baseFieldName = 'title', $custom = false) { // check if URL is available from the referene if (!Spoon::exists('url')) { throw new BackendException('URL should be available in the reference.'); } // get BackendURL instance $this->URL = Spoon::get('url'); // should we use meta-custom $this->custom = (bool) $custom; // set form instance $this->frm = $form; // set base field name $this->baseFieldName = (string) $baseFieldName; // metaId was specified, so we should load the item if ($metaId !== null) { $this->loadMeta($metaId); } // load the form $this->loadForm(); }
/** * Returns the mailchimp object. * * @return mailchimp */ public static function getMC() { // mailchimp reference exists if (!\Spoon::exists('mailchimp')) { // check if the mailchimp class exists if (!\SpoonFile::exists(PATH_LIBRARY . '/external/mcapi.php')) { // the class doesn't exist, so throw an exception throw new \SpoonFileException(sprintf(FL::err('ClassDoesNotExist'), 'mailchimp')); } // require mailchimp class require_once PATH_LIBRARY . '/external/mcapi.php'; // set login data $key = FrontendModel::getModuleSetting('MailMotor', 'api_key'); if (empty($key)) { throw new \Exception('Mailmotor api_key is required.'); } // init mailchimp object $mc = new \MCAPI($key); // set mailchimp object reference \Spoon::set('mailchimp', $mc); } // return the CampaignMonitor object return \Spoon::get('mailchimp'); }
/** * Assign the labels */ private function parseLabels() { // grab the current module if (Spoon::exists('url')) { $currentModule = Spoon::get('url')->getModule(); } elseif (isset($_GET['module']) && $_GET['module'] != '') { $currentModule = (string) $_GET['module']; } else { $currentModule = 'core'; } // init vars $realErrors = array(); $realLabels = array(); $realMessages = array(); // get all errors $errors = BackendLanguage::getErrors(); // get all labels $labels = BackendLanguage::getLabels(); // get all messages $messages = BackendLanguage::getMessages(); // set the begin state $realErrors = $errors['core']; $realLabels = $labels['core']; $realMessages = $messages['core']; // loop all errors, label, messages and add them again, but prefixed with Core. So we can decide in the // template to use the core-value instead of the one set by the module foreach ($errors['core'] as $key => $value) { $realErrors['Core' . $key] = $value; } foreach ($labels['core'] as $key => $value) { $realLabels['Core' . $key] = $value; } foreach ($messages['core'] as $key => $value) { $realMessages['Core' . $key] = $value; } // are there errors for the current module? if (isset($errors[$currentModule])) { // loop the module-specific errors and reset them in the array with values we will use foreach ($errors[$currentModule] as $key => $value) { $realErrors[$key] = $value; } } // are there labels for the current module? if (isset($labels[$currentModule])) { // loop the module-specific labels and reset them in the array with values we will use foreach ($labels[$currentModule] as $key => $value) { $realLabels[$key] = $value; } } // are there messages for the current module? if (isset($messages[$currentModule])) { // loop the module-specific errors and reset them in the array with values we will use foreach ($messages[$currentModule] as $key => $value) { $realMessages[$key] = $value; } } // execute addslashes on the values for the locale, will be used in JS if ($this->addSlashes) { foreach ($realErrors as &$value) { $value = addslashes($value); } foreach ($realLabels as &$value) { $value = addslashes($value); } foreach ($realMessages as &$value) { $value = addslashes($value); } } // sort the arrays (just to make it look beautifull) ksort($realErrors); ksort($realLabels); ksort($realMessages); // assign errors $this->assignArray($realErrors, 'err'); // assign labels $this->assignArray($realLabels, 'lbl'); // assign messages $this->assignArray($realMessages, 'msg'); }
/** * Get a message from the language-file * * @param string $key The key to get. * @param string[optional] $module The module wherin we should search. * @return string */ public static function getMessage($key, $module = null) { if ($module === null) { if (Spoon::exists('url')) { $module = Spoon::get('url')->getModule(); } elseif (isset($_GET['module']) && $_GET['module'] != '') { $module = (string) $_GET['module']; } else { $module = 'core'; } } $key = SpoonFilter::toCamelCase((string) $key); $module = (string) $module; // check if the message exists if (isset(self::$msg[$module][$key])) { return self::$msg[$module][$key]; } // check if the message exists in the core if (isset(self::$msg['core'][$key])) { return self::$msg['core'][$key]; } // otherwise return the key in label-format return '{$msg' . SpoonFilter::toCamelCase($module) . $key . '}'; }
/** * Returns the CampaignMonitor object. * * @return CampaignMonitor */ public static function getCM() { // campaignmonitor reference exists if (!Spoon::exists('campaignmonitor')) { // check if the CampaignMonitor class exists if (!SpoonFile::exists(PATH_LIBRARY . '/external/campaignmonitor.php')) { // the class doesn't exist, so throw an exception throw new SpoonFileException(BL::err('ClassDoesNotExist', 'mailmotor')); } // require CampaignMonitor class require_once 'external/campaignmonitor.php'; // set login data $url = BackendModel::getModuleSetting('mailmotor', 'cm_url'); $username = BackendModel::getModuleSetting('mailmotor', 'cm_username'); $password = BackendModel::getModuleSetting('mailmotor', 'cm_password'); // init CampaignMonitor object $cm = new CampaignMonitor($url, $username, $password, 60, self::getClientId()); // set CampaignMonitor object reference Spoon::set('campaignmonitor', $cm); } return Spoon::get('campaignmonitor'); }
/** * Sets all the default settings needed when attempting to use sorting */ private function setSortingOptions() { // default URL if (Spoon::exists('url')) { $this->setURL(BackendModel::createURLForAction(null, null, null, array('offset' => '[offset]', 'order' => '[order]', 'sort' => '[sort]'), false)); } // sorting labels $this->setSortingLabels(BL::lbl('SortAscending'), BL::lbl('SortedAscending'), BL::lbl('SortDescending'), BL::lbl('SortedDescending')); }
/** * Get a message from the language-file * * @return string * @param string $key The key to get. * @param string[optional] $module The module wherin we should search. */ public static function getMessage($key, $module = null) { // do we know the module if ($module === null) { if (Spoon::exists('url')) { $module = Spoon::get('url')->getModule(); } elseif (isset($_GET['module']) && $_GET['module'] != '') { $module = (string) $_GET['module']; } else { $module = 'core'; } } // redefine $key = (string) $key; $module = (string) $module; // if the error exists return it, if (isset(self::$msg[$module][$key])) { return self::$msg[$module][$key]; } // if it exists in the core-errors if (isset(self::$msg['core'][$key])) { return self::$msg['core'][$key]; } // otherwise return the key in label-format return '{$msg' . SpoonFilter::toCamelCase($module) . $key . '}'; }
/** * Parse a field and return the HTML. * * @return string * @param array $field Field data. */ public static function parseField(array $field) { // got a field if (!empty($field)) { // init $frm = new BackendForm('tmp', ''); $tpl = Spoon::exists('template') ? Spoon::get('template') : new BackendTemplate(); $fieldHTML = ''; $fieldName = 'field' . $field['id']; $values = isset($field['settings']['values']) ? $field['settings']['values'] : null; $defaultValues = isset($field['settings']['default_values']) ? $field['settings']['default_values'] : null; /** * Create form and parse to HTML */ // dropdown if ($field['type'] == 'dropdown') { // get index of selected item $defaultIndex = array_search($defaultValues, $values, true); if ($defaultIndex === false) { $defaultIndex = null; } // create element $ddm = $frm->addDropdown($fieldName, $values, $defaultIndex); // empty default element $ddm->setDefaultElement(''); // get content $fieldHTML = $ddm->parse(); } elseif ($field['type'] == 'radiobutton') { // rebuild values foreach ($values as $value) { $newValues[] = array('label' => $value, 'value' => $value); } // create element $rbt = $frm->addRadiobutton($fieldName, $newValues, $defaultValues); // get content $fieldHTML = $rbt->parse(); } elseif ($field['type'] == 'checkbox') { // rebuild values foreach ($values as $value) { $newValues[] = array('label' => $value, 'value' => $value); } // create element $chk = $frm->addMultiCheckbox($fieldName, $newValues, $defaultValues); // get content $fieldHTML = $chk->parse(); } elseif ($field['type'] == 'textbox') { // create element $txt = $frm->addText($fieldName, $defaultValues); $txt->setAttribute('disabled', 'disabled'); // get content $fieldHTML = $txt->parse(); } elseif ($field['type'] == 'textarea') { // create element $txt = $frm->addTextarea($fieldName, $defaultValues); $txt->setAttribute('cols', 30); $txt->setAttribute('disabled', 'disabled'); // get content $fieldHTML = $txt->parse(); } elseif ($field['type'] == 'heading') { $fieldHTML = '<h3>' . $values . '</h3>'; } elseif ($field['type'] == 'paragraph') { $fieldHTML = $values; } /** * Parse the field into the template */ // init $tpl->assign('plaintext', false); $tpl->assign('simple', false); $tpl->assign('multiple', false); $tpl->assign('id', $field['id']); $tpl->assign('required', isset($field['validations']['required'])); // plaintext items if ($field['type'] == 'heading' || $field['type'] == 'paragraph') { // assign $tpl->assign('content', $fieldHTML); $tpl->assign('plaintext', true); } elseif ($field['type'] == 'checkbox' || $field['type'] == 'radiobutton') { // name (prefixed by type) $name = $field['type'] == 'checkbox' ? 'chk' . ucfirst($fieldName) : 'rbt' . ucfirst($fieldName); // rebuild so the html is stored in a general name (and not rbtName) foreach ($fieldHTML as &$item) { $item['field'] = $item[$name]; } // show multiple $tpl->assign('label', $field['settings']['label']); $tpl->assign('items', $fieldHTML); $tpl->assign('multiple', true); } else { // assign $tpl->assign('label', $field['settings']['label']); $tpl->assign('field', $fieldHTML); $tpl->assign('simple', true); } // cough up created html return $tpl->getContent(BACKEND_MODULE_PATH . '/layout/templates/field.tpl'); } else { return ''; } }
/** * Get (or create and get) a database-connection * @later split the write and read connection * * @param bool[optional] $write Do you want the write-connection or not? * @return SpoonDatabase */ public static function getDB($write = false) { $write = (bool) $write; // do we have a db-object ready? if (!Spoon::exists('database')) { // create instance $db = new SpoonDatabase(DB_TYPE, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE, DB_PORT); // utf8 compliance & MySQL-timezone $db->execute('SET CHARACTER SET utf8, NAMES utf8, time_zone = "+0:00"'); // store Spoon::set('database', $db); } // return db-object return Spoon::get('database'); }