150 lines
4.6 KiB
PHP
Executable File
150 lines
4.6 KiB
PHP
Executable File
<?php declare(strict_types=1); namespace IR\App\Webservices; if (!defined('IR_START')) exit('<pre>No direct script access allowed</pre>');
|
|
/**
|
|
* @framework iResponse Framework
|
|
* @version 1.0
|
|
* @author Amine Idrissi <contact@iresponse.tech>
|
|
* @date 2019
|
|
* @name Affiliate.php
|
|
*/
|
|
|
|
# core
|
|
use IR\Core\Base as Base;
|
|
use IR\Core\Application as Application;
|
|
|
|
# helpers
|
|
use IR\App\Helpers\Authentication as Authentication;
|
|
|
|
use IR\App\Helpers\Page as Page;
|
|
use IR\App\Helpers\Permissions;
|
|
use IR\App\Models\Admin\Domain;
|
|
use IR\App\Models\Admin\MtaServer;
|
|
use IR\App\Models\Admin\ShortLink;
|
|
use IR\App\Models\Admin\User;
|
|
use IR\App\Models\Affiliate\Link;
|
|
use IR\Exceptions\Types\PageException;
|
|
|
|
/**
|
|
* @name Affiliate
|
|
* @description Affiliate WebService
|
|
*/
|
|
class ShortLinks extends Base
|
|
{
|
|
/**
|
|
* @app
|
|
* @readwrite
|
|
*/
|
|
protected $app;
|
|
|
|
|
|
/**
|
|
* @app
|
|
* @readwrite
|
|
*/
|
|
protected $authenticatedUser;
|
|
|
|
|
|
/**
|
|
* @name init
|
|
* @description initializing process before the action method executed
|
|
* @once
|
|
* @protected
|
|
*/
|
|
public function init()
|
|
{
|
|
# set the current application to a local variable
|
|
$this->app = Application::getCurrent();
|
|
|
|
$this->app->database('system')->connect();
|
|
# check for authentication
|
|
if (!Authentication::isUserAuthenticated()) {
|
|
Page::redirect($this->app->http->request->getBaseURL() . RDS . 'auth' . RDS . 'login.' . DEFAULT_EXTENSION);
|
|
}
|
|
|
|
# check users roles
|
|
Authentication::checkUserRoles();
|
|
|
|
# get the authenticated user
|
|
$this->authenticatedUser = Authentication::getAuthenticatedUser();
|
|
}
|
|
|
|
|
|
/**
|
|
* @name save
|
|
* @description save a short link
|
|
* @before init
|
|
*/
|
|
public function save($parameters = [])
|
|
{
|
|
# check for permissions
|
|
$access = Permissions::customcheckForAuthorization($this->authenticatedUser, __CLASS__, 'save');
|
|
|
|
if ($access == false) {
|
|
throw new PageException('Access Denied !', 403);
|
|
}
|
|
|
|
$domainId = $parameters['domain-id'];
|
|
$url = $parameters['url'];
|
|
$randomWord = $parameters['randomWord'];
|
|
$size = intval($parameters['size']);
|
|
$valuesNumber = intval($parameters['valuesNumber']);
|
|
// if the url an image add it
|
|
$urlExt = pathinfo($url, PATHINFO_EXTENSION);
|
|
$imgExts = array("gif", "jpg", "jpeg", "png", "tiff", "tif");
|
|
if (in_array($urlExt, $imgExts))
|
|
{
|
|
$values = $this->createShortLink($url, $domainId, $randomWord, $size, $valuesNumber);
|
|
Page::printApiResults(200,'',['values' => $values]);
|
|
}
|
|
else
|
|
{
|
|
// if the url is not an image , check if the url exist in the application
|
|
$link = Link::first(Link::FETCH_ARRAY, ['status = ? and value = ?', ['Activated',$url]]);
|
|
if (empty($link))
|
|
{
|
|
$values = $this->createShortLink($url, $domainId, 'suspicion', $size, $valuesNumber);
|
|
Page::printApiResults(403,"Link doesn't exist in our system");
|
|
}
|
|
else {
|
|
$values = $this->createShortLink($url, $domainId, $randomWord, $size, $valuesNumber);
|
|
Page::printApiResults(200,'',['values' => $values]);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
private function createShortLink($url, $domainId, $randomWord, $size, $valuesNumber)
|
|
{
|
|
$values = [];
|
|
for($i = 0; $i < $valuesNumber; $i++)
|
|
{
|
|
$shortLink = new ShortLink();
|
|
$username = $this->authenticatedUser->getEmail();
|
|
$shortLink->setCreatedBy($username);
|
|
$shortLink->setCreatedDate(date('Y-m-d'));
|
|
$shortLink->setLastUpdatedBy($username);
|
|
$shortLink->setLastUpdatedDate(date('Y-m-d'));
|
|
$shortLink->setUrl($url);
|
|
// Set the value
|
|
$domain = Domain::first(Domain::FETCH_ARRAY,['status = ? and id = ?',['Activated',$domainId]]);
|
|
$randomString = $this->app->utils->strings->random($size,true,true,true,false);
|
|
if ($randomWord !== "")
|
|
{
|
|
$value = "http://{$domain['value']}/{$randomWord}_{$randomString}";
|
|
}
|
|
else
|
|
{
|
|
$value = "http://{$domain['value']}/{$randomString}";
|
|
}
|
|
$values[] = $value;
|
|
$shortLink->setValue($value);
|
|
$server = MtaServer::first(MtaServer::FETCH_ARRAY,['status = ?',['Activated']]);
|
|
$shortLink->setServerId(intval($server['id']));
|
|
$shortLink->setDomainId(intval($domainId));
|
|
$shortLink->setServerName($server['name']);
|
|
$shortLink->setDomainName($domain['value']);
|
|
$shortLink->insert();
|
|
}
|
|
return $values;
|
|
}
|
|
}
|