This source file can be used to communicate with ForkAPI (http://api.fork-cms.be)
The class is documented in the file itself. If you find any bugs help me out and report them. Reporting can be done by sending an email to php-fork-api-bugs[at]verkoyen[dot]eu.
If you report a bug, make sure you give me enough information (include your code).
License
Copyright (c) 2010, Tijs Verkoyen. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.
This software is provided by the author "as is" and any express or implied warranties, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose are disclaimed. In no event shall the author be liable for any direct, indirect, incidental, special, exemplary, or consequential damages (including, but not limited to, procurement of substitute goods or services; loss of use, data, or profits; or business interruption) however caused and on any theory of liability, whether in contract, strict liability, or tort (including negligence or otherwise) arising in any way out of the use of this software, even if advised of the possibility of such damage.
/** * Add a device to a user. * * @return void * @param string $token The token of the device. * @param string $email The emailaddress for the user to link the device to. */ public static function appleAdddevice($token, $email) { // authorized? if (API::authorize()) { // redefine $token = str_replace(' ', '', (string) $token); // validate if ($token == '') { API::output(API::BAD_REQUEST, array('message' => 'No token-parameter provided.')); } if ($email == '') { API::output(API::BAD_REQUEST, array('message' => 'No email-parameter provided.')); } // we should tell the ForkAPI that we registered a device $publicKey = BackendModel::getModuleSetting('core', 'fork_api_public_key', ''); $privateKey = FrontendModel::getModuleSetting('core', 'fork_api_private_key', ''); // validate keys if ($publicKey == '' || $privateKey == '') { API::output(API::BAD_REQUEST, array('message' => 'Invalid key for the Fork API, configer them in the backend.')); } try { // load user $user = new BackendUser(null, $email); // get current tokens $tokens = (array) $user->getSetting('apple_device_token'); // not already in array? if (!in_array($token, $tokens)) { $tokens[] = $token; } // require the class require_once PATH_LIBRARY . '/external/fork_api.php'; // create instance $forkAPI = new ForkAPI($publicKey, $privateKey); // make the call $forkAPI->appleRegisterDevice($token); // store if (!empty($tokens)) { $user->setSetting('apple_device_token', $tokens); } } catch (Exception $e) { API::output(API::FORBIDDEN, array('message' => 'Can\'t authenticate you.')); } } }
/** * Add a Microsoft device to a user. * * @param string $uri The uri of the channel opened for the device. * @param string $email The emailaddress for the user to link the device to. */ public static function microsoftAddDevice($uri, $email) { if (BaseAPI::isAuthorized()) { // redefine $uri = (string) $uri; // validate if ($uri == '') { BaseAPI::output(BaseAPI::BAD_REQUEST, array('message' => 'No uri-parameter provided.')); } if ($email == '') { BaseAPI::output(BaseAPI::BAD_REQUEST, array('message' => 'No email-parameter provided.')); } // we should tell the ForkAPI that we registered a device $publicKey = Model::get('fork.settings')->get('Core', 'fork_api_public_key', ''); $privateKey = Model::get('fork.settings')->get('Core', 'fork_api_private_key', ''); // validate keys if ($publicKey == '' || $privateKey == '') { BaseAPI::output(BaseAPI::BAD_REQUEST, array('message' => 'Invalid key for the Fork API, configure them in the backend.')); } try { // load user $user = new User(null, $email); // get current uris $uris = (array) $user->getSetting('microsoft_channel_uri'); // not already in array? if (!in_array($uri, $uris)) { $uris[] = $uri; } // require the class require_once PATH_LIBRARY . '/external/fork_api.php'; // create instance $forkAPI = new \ForkAPI($publicKey, $privateKey); // make the call $forkAPI->microsoftRegisterDevice($uris); // store if (!empty($uris)) { $user->setSetting('microsoft_channel_uri', $uris); } } catch (Exception $e) { BaseAPI::output(BaseAPI::FORBIDDEN, array('message' => 'Can\'t authenticate you.')); } } }
/** * Store the settings */ private function setSettings() { // languages settings $this->setSetting('core', 'languages', $this->getLanguages(), true); $this->setSetting('core', 'active_languages', $this->getLanguages(), true); $this->setSetting('core', 'redirect_languages', $this->getLanguages(), true); $this->setSetting('core', 'default_language', $this->getVariable('default_language'), true); $this->setSetting('core', 'interface_languages', $this->getInterfaceLanguages(), true); $this->setSetting('core', 'default_interface_language', $this->getVariable('default_interface_language'), true); // other settings $this->setSetting('core', 'theme'); $this->setSetting('core', 'akismet_key', ''); $this->setSetting('core', 'google_maps_key', ''); $this->setSetting('core', 'max_num_revisions', 20); $this->setSetting('core', 'site_domains', array($this->getVariable('site_domain'))); $this->setSetting('core', 'site_html_header', ''); $this->setSetting('core', 'site_html_footer', ''); // date & time $this->setSetting('core', 'date_format_short', 'j.n.Y'); $this->setSetting('core', 'date_formats_short', array('j/n/Y', 'j-n-Y', 'j.n.Y', 'n/j/Y', 'n/j/Y', 'n/j/Y', 'd/m/Y', 'd-m-Y', 'd.m.Y', 'm/d/Y', 'm-d-Y', 'm.d.Y', 'j/n/y', 'j-n-y', 'j.n.y', 'n/j/y', 'n-j-y', 'n.j.y', 'd/m/y', 'd-m-y', 'd.m.y', 'm/d/y', 'm-d-y', 'm.d.y')); $this->setSetting('core', 'date_format_long', 'l j F Y'); $this->setSetting('core', 'date_formats_long', array('j F Y', 'D j F Y', 'l j F Y', 'j F, Y', 'D j F, Y', 'l j F, Y', 'd F Y', 'd F, Y', 'F j Y', 'D F j Y', 'l F j Y', 'F d, Y', 'D F d, Y', 'l F d, Y')); $this->setSetting('core', 'time_format', 'H:i'); $this->setSetting('core', 'time_formats', array('H:i', 'H:i:s', 'g:i a', 'g:i A')); // number formats $this->setSetting('core', 'number_format', 'dot_nothing'); $this->setSetting('core', 'number_formats', array('comma_nothing' => '10000,25', 'dot_nothing' => '10000.25', 'dot_comma' => '10,000.25', 'comma_dot' => '10.000,25', 'dot_space' => '10000.25', 'comma_space' => '10 000,25')); // e-mail settings $this->setSetting('core', 'mailer_from', array('name' => 'Fork CMS', 'email' => $this->getVariable('spoon_debug_email'))); $this->setSetting('core', 'mailer_to', array('name' => 'Fork CMS', 'email' => $this->getVariable('spoon_debug_email'))); $this->setSetting('core', 'mailer_reply_to', array('name' => 'Fork CMS', 'email' => $this->getVariable('spoon_debug_email'))); // stmp settings $this->setSetting('core', 'smtp_server', $this->getVariable('smtp_server')); $this->setSetting('core', 'smtp_port', $this->getVariable('smtp_port')); $this->setSetting('core', 'smtp_username', $this->getVariable('smtp_username')); $this->setSetting('core', 'smtp_password', $this->getVariable('smtp_password')); // default titles $siteTitles = array('en' => 'My website', 'cn' => '我的网站', 'nl' => 'Mijn website', 'fr' => 'Mon site web', 'de' => 'Meine Webseite', 'hu' => 'Hhonlapom', 'it' => 'Il mio sito web', 'ru' => 'мой сайт', 'es' => 'Mi sitio web'); // language specific foreach ($this->getLanguages() as $language) { // set title $this->setSetting('core', 'site_title_' . $language, isset($siteTitles[$language]) ? $siteTitles[$language] : $this->getVariable('site_title')); } /* * We're going to try to install the settings for the api. */ require_once PATH_LIBRARY . '/external/fork_api.php'; // create new instance $api = new ForkAPI(); try { // get the keys $keys = $api->coreRequestKeys($this->getVariable('site_domain'), $this->getVariable('api_email')); // api settings $this->setSetting('core', 'fork_api_public_key', $keys['public']); $this->setSetting('core', 'fork_api_private_key', $keys['private']); // set keys $api->setPublicKey($keys['public']); $api->setPrivateKey($keys['private']); // get services $services = (array) $api->pingGetServices(); // set services if (!empty($services)) { $this->setSetting('core', 'ping_services', array('services' => $services, 'date' => time())); } } catch (Exception $e) { // we don't need those keys. } // ckfinder $this->setSetting('core', 'ckfinder_license_name', 'Fork CMS'); $this->setSetting('core', 'ckfinder_license_key', 'QJH2-32UV-6VRM-V6Y7-A91J-W26Z-3F8R'); }
/** * Push a notification to Apple's notifications-server * * @param mixed $alert The message/dictionary to send. * @param int $badge The number for the badge. * @param string $sound The sound that should be played. * @param array $extraDictionaries Extra dictionaries. */ public static function pushToAppleApp($alert, $badge = null, $sound = null, array $extraDictionaries = null) { // get ForkAPI-keys $publicKey = self::get('fork.settings')->get('Core', 'fork_api_public_key', ''); $privateKey = self::get('fork.settings')->get('Core', 'fork_api_private_key', ''); // no keys, so stop here if ($publicKey == '' || $privateKey == '') { return; } // get all apple-device tokens $deviceTokens = (array) self::getContainer()->get('database')->getColumn('SELECT s.value FROM users AS i INNER JOIN users_settings AS s WHERE i.active = ? AND i.deleted = ? AND s.name = ? AND s.value != ?', array('Y', 'N', 'apple_device_token', 'N;')); // no devices, so stop here if (empty($deviceTokens)) { return; } // init var $tokens = array(); // loop devices foreach ($deviceTokens as $row) { // unserialize $row = unserialize($row); // loop and add foreach ($row as $item) { $tokens[] = $item; } } // no tokens, so stop here if (empty($tokens)) { return; } // require the class require_once PATH_LIBRARY . '/external/fork_api.php'; // create instance $forkAPI = new \ForkAPI($publicKey, $privateKey); try { // push $response = $forkAPI->applePush($tokens, $alert, $badge, $sound, $extraDictionaries); if (!empty($response)) { // get db $db = self::getContainer()->get('database'); // loop the failed keys and remove them foreach ($response as $deviceToken) { // get setting wherein the token is available $row = $db->getRecord('SELECT i.* FROM users_settings AS i WHERE i.name = ? AND i.value LIKE ?', array('apple_device_token', '%' . $deviceToken . '%')); // any rows? if (!empty($row)) { // reset data $data = unserialize($row['value']); // loop keys foreach ($data as $key => $token) { // match and unset if needed. if ($token == $deviceToken) { unset($data[$key]); } } // no more tokens left? if (empty($data)) { $db->delete('users_settings', 'user_id = ? AND name = ?', array($row['user_id'], $row['name'])); } else { $db->update('users_settings', array('value' => serialize($data)), 'user_id = ? AND name = ?', array($row['user_id'], $row['name'])); } } } } } catch (Exception $e) { if (self::getContainer()->getParameter('kernel.debug')) { throw $e; } } }
/** * Ping the known webservices * * @param string $pageOrFeedURL The page/feed that has changed. * @param string $category An optional category for the site. * @return bool If everything went fne true will, otherwise false. */ public static function ping($pageOrFeedURL = null, $category = null) { $siteTitle = self::get('fork.settings')->get('Core', 'site_title_' . Language::getWorkingLanguage(), SITE_DEFAULT_TITLE); $siteURL = SITE_URL; $pageOrFeedURL = $pageOrFeedURL !== null ? (string) $pageOrFeedURL : null; $category = $category !== null ? (string) $category : null; // get ping services $pingServices = self::get('fork.settings')->get('Core', 'ping_services', null); // no ping services available or older than one month ago if ($pingServices === null || $pingServices['date'] < strtotime('-1 month')) { // get ForkAPI-keys $publicKey = self::get('fork.settings')->get('Core', 'fork_api_public_key', ''); $privateKey = self::get('fork.settings')->get('Core', 'fork_api_private_key', ''); // validate keys if ($publicKey == '' || $privateKey == '') { return false; } // require the class require_once PATH_LIBRARY . '/external/fork_api.php'; // create instance $forkAPI = new \ForkAPI($publicKey, $privateKey); // try to get the services try { $pingServices['services'] = $forkAPI->pingGetServices(); $pingServices['date'] = time(); } catch (Exception $e) { // check if the error should not be ignored if (strpos($e->getMessage(), 'Operation timed out') === false && strpos($e->getMessage(), 'Invalid headers') === false) { if (BackendModel::getContainer()->getParameter('kernel.debug')) { throw $e; } else { // stop, hammertime return false; } } } // store the services self::get('fork.settings')->set('Core', 'ping_services', $pingServices); } // make sure services array will not trigger an error (even if we couldn't load any) if (!isset($pingServices['services']) || !$pingServices['services']) { $pingServices['services'] = array(); } // loop services foreach ($pingServices['services'] as $service) { $client = new \SpoonXMLRPCClient($service['url']); $client->setUserAgent('Fork ' . FORK_VERSION); $client->setTimeOut(10); $client->setPort($service['port']); try { // extended ping? if ($service['type'] == 'extended') { // no page or feed URL present? if ($pageOrFeedURL === null) { continue; } $parameters[] = array('type' => 'string', 'value' => $siteTitle); $parameters[] = array('type' => 'string', 'value' => $siteURL); $parameters[] = array('type' => 'string', 'value' => $pageOrFeedURL); if ($category !== null) { $parameters[] = array('type' => 'string', 'value' => $category); } $client->execute('weblogUpdates.extendedPing', $parameters); } else { // default ping $parameters[] = array('type' => 'string', 'value' => $siteTitle); $parameters[] = array('type' => 'string', 'value' => $siteURL); $client->execute('weblogUpdates.ping', $parameters); } } catch (Exception $e) { // check if the error should not be ignored if (strpos($e->getMessage(), 'Operation timed out') === false && strpos($e->getMessage(), 'Invalid headers') === false) { if (BackendModel::getContainer()->getParameter('kernel.debug')) { throw $e; } } continue; } } return true; }
/** * Push a notification to Microsoft's notifications-server * * @param string $title The title for the tile to send. * @param string[optional] $count The count for the tile to send. * @param string[optional] $image The image for the tile to send. * @param string[optional] $backTitle The title for the tile backtround to send. * @param string[optional] $backText The text for the tile background to send. * @param string[optional] $backImage The image for the tile background to send. * @param string[optional] $tile The secondary tile to update. * @param string[optional] $uri The application uri to navigate to. */ public static function pushToMicrosoftApp($title, $count = null, $image = null, $backTitle = null, $backText = null, $backImage = null, $tile = null, $uri = null) { // get ForkAPI-keys $publicKey = FrontendModel::getModuleSetting('core', 'fork_api_public_key', ''); $privateKey = FrontendModel::getModuleSetting('core', 'fork_api_private_key', ''); // no keys, so stop here if ($publicKey == '' || $privateKey == '') { return; } // get all microsoft channel uri's $channelUris = (array) FrontendModel::getDB()->getColumn('SELECT s.value FROM users AS i INNER JOIN users_settings AS s WHERE i.active = ? AND i.deleted = ? AND s.name = ? AND s.value != ?', array('Y', 'N', 'microsoft_channel_uri', 'N;')); // no devices, so stop here if (empty($channelUris)) { return; } // init var $uris = array(); // loop devices foreach ($channelUris as $row) { // unserialize $row = unserialize($row); // loop and add foreach ($row as $item) { $uris[] = $item; } } // no channel uri's, so stop here if (empty($uris)) { return; } // require the class require_once PATH_LIBRARY . '/external/fork_api.php'; // create instance $forkAPI = new ForkAPI($publicKey, $privateKey); try { // push $forkAPI->microsoftPush($uris, $title, $count, $image, $backTitle, $backText, $backImage, $tile, $uri); } catch (Exception $e) { if (SPOON_DEBUG) { throw $e; } } }
/** * Store the settings */ private function setSettings() { // languages settings $this->setSetting('Core', 'languages', $this->getLanguages(), true); $this->setSetting('Core', 'active_languages', $this->getLanguages(), true); $this->setSetting('Core', 'redirect_languages', $this->getLanguages(), true); $this->setSetting('Core', 'default_language', $this->getVariable('default_language'), true); $this->setSetting('Core', 'interface_languages', $this->getInterfaceLanguages(), true); $this->setSetting('Core', 'default_interface_language', $this->getVariable('default_interface_language'), true); // other settings $this->setSetting('Core', 'theme'); $this->setSetting('Core', 'akismet_key', ''); $this->setSetting('Core', 'google_maps_key', ''); $this->setSetting('Core', 'max_num_revisions', 20); $this->setSetting('Core', 'site_domains', array($this->getVariable('site_domain'))); $this->setSetting('Core', 'site_html_header', ''); $this->setSetting('Core', 'site_html_footer', ''); // date & time $this->setSetting('Core', 'date_format_short', 'j.n.Y'); $this->setSetting('Core', 'date_formats_short', array('j/n/Y', 'j-n-Y', 'j.n.Y', 'n/j/Y', 'n/j/Y', 'n/j/Y', 'd/m/Y', 'd-m-Y', 'd.m.Y', 'm/d/Y', 'm-d-Y', 'm.d.Y', 'j/n/y', 'j-n-y', 'j.n.y', 'n/j/y', 'n-j-y', 'n.j.y', 'd/m/y', 'd-m-y', 'd.m.y', 'm/d/y', 'm-d-y', 'm.d.y')); $this->setSetting('Core', 'date_format_long', 'l j F Y'); $this->setSetting('Core', 'date_formats_long', array('j F Y', 'D j F Y', 'l j F Y', 'j F, Y', 'D j F, Y', 'l j F, Y', 'd F Y', 'd F, Y', 'F j Y', 'D F j Y', 'l F j Y', 'F d, Y', 'D F d, Y', 'l F d, Y')); $this->setSetting('Core', 'time_format', 'H:i'); $this->setSetting('Core', 'time_formats', array('H:i', 'H:i:s', 'g:i a', 'g:i A')); // number formats $this->setSetting('Core', 'number_format', 'dot_nothing'); $this->setSetting('Core', 'number_formats', array('comma_nothing' => '10000,25', 'dot_nothing' => '10000.25', 'dot_comma' => '10,000.25', 'comma_dot' => '10.000,25', 'dot_space' => '10000.25', 'comma_space' => '10 000,25')); // e-mail settings $this->setSetting('Core', 'mailer_from', array('name' => 'Fork CMS', 'email' => $this->getVariable('spoon_debug_email'))); $this->setSetting('Core', 'mailer_to', array('name' => 'Fork CMS', 'email' => $this->getVariable('spoon_debug_email'))); $this->setSetting('Core', 'mailer_reply_to', array('name' => 'Fork CMS', 'email' => $this->getVariable('spoon_debug_email'))); // stmp settings $this->setSetting('Core', 'smtp_server', $this->getVariable('smtp_server')); $this->setSetting('Core', 'smtp_port', $this->getVariable('smtp_port')); $this->setSetting('Core', 'smtp_username', $this->getVariable('smtp_username')); $this->setSetting('Core', 'smtp_password', $this->getVariable('smtp_password')); // default titles $siteTitles = array('en' => 'My website', 'bg' => 'уебсайта си', 'zh' => '我的网站', 'cs' => 'můj web', 'nl' => 'Mijn website', 'fr' => 'Mon site web', 'de' => 'Meine Webseite', 'el' => 'ιστοσελίδα μου', 'hu' => 'Hhonlapom', 'it' => 'Il mio sito web', 'ja' => '私のウェブサイト', 'lt' => 'mano svetainė', 'pl' => 'moja strona', 'ro' => 'site-ul meu', 'ru' => 'мой сайт', 'es' => 'Mi sitio web', 'sv' => 'min hemsida', 'tr' => 'web siteme', 'uk' => 'мій сайт'); // language specific foreach ($this->getLanguages() as $language) { // set title $this->setSetting('Core', 'site_title_' . $language, isset($siteTitles[$language]) ? $siteTitles[$language] : $this->getVariable('site_title')); } // @TODO this should be removed when the api is kicked out // create new instance require_once PATH_LIBRARY . '/external/fork_api.php'; $api = new \ForkAPI(); try { // get the keys $keys = $api->coreRequestKeys($this->getVariable('site_domain'), $this->getVariable('api_email')); // api settings $this->setSetting('Core', 'fork_api_public_key', $keys['public']); $this->setSetting('Core', 'fork_api_private_key', $keys['private']); // set keys $api->setPublicKey($keys['public']); $api->setPrivateKey($keys['private']); // get services $services = (array) $api->pingGetServices(); // set services if (!empty($services)) { $this->setSetting('Core', 'ping_services', array('services' => $services, 'date' => time())); } } catch (\Exception $e) { // we don't need those keys. } // ckfinder $this->setSetting('Core', 'ckfinder_license_name', 'Fork CMS'); $this->setSetting('Core', 'ckfinder_license_key', 'VNA6-BP17-T7D3-CP1B-EMJF-X7Q3-5THF'); }