<commit_before><?php
namespace addons\content_api\v1\home;
use \lib\utility\permission;
use \lib\utility;
use \lib\debug;
use \addons\content_enter\main\tools\token;

class model extends \mvc\model
{

	/**
	 * the user id
	 *
	 * @var        integer
	 */
	public $user_id = null;


	/**
	 * the api is telegram bot
	 *
	 * @var        boolean
	 */
	public $telegram_api_mode = false;


	/**
	 * make debug return
	 * default is true
	 * in some where in site this method is false
	 *
	 * @var        boolean
	 */
	public $debug = true;


	/**
	 * the url
	 *
	 * @var        <type>
	 */
	public $url = null;


	/**
	 * the authorization
	 *
	 * @var        <type>
	 */
	public $authorization          = null;


	/**
	 * the parent api key
	 *
	 * @var        <type>
	 */
	public $parent_api_key         = null;
	public $parent_api_key_user_id = 0;


	use tools\_use;


	/**
	 * { function_description }
	 *
	 * @param      <type>  $_name  The name
	 * @param      <type>  $_args  The arguments
	 * @param      <type>  $parm   The parameter
	 *
	 * @return     <type>  ( description_of_the_return_value )
	 */
	function _call($_name, $_args, $parm)
	{
		$this->url = \lib\router::get_url();

		\lib\temp::set('api', true);

		$this->api_key();

		if(!debug::$status)
		{
			$this->_processor(['force_stop' => true]);
		}

		return parent::_call($_name, $_args, $parm);
	}


	/**
	 * check api key and set the user id
	 */
	public function api_key()
	{

		$api_token = utility::header("api_token") ? utility::header("api_token") : utility::header("Api_token");

		$authorization = utility::header("authorization");
		if($api_token)
		{
			$authorization = $api_token;
		}
		elseif(!$authorization)
		{
			$authorization = utility::header("Authorization");
		}

		if(!$authorization)
		{
			return debug::error('Authorization not found', 'authorization', 'access');
		}

		if($authorization === \lib\option::config('enter','telegram_hook'))
		{
			$this->telegram_api_mode = true;
			$this->telegram_token();
		}
		else
		{
			$token = token::get_type($authorization);

			if(!debug::$status)
			{
				return false;
			}

			switch ($token)
			{

				case 'user_token':
				case 'guest':
					if($this->url == 'v1/token/login' || $this->url == 'v1/token/guest')
					{
						debug::error(T_("Access denide (Invalid url)"), 'authorization', 'access');
						return false;
					}

					if(!token::check($authorization, $token))
					{
						return false;
					}

					$user_id = token::get_user_id($authorization);

					if(!$user_id)
					{
						debug::error(T_("Invalid authorization key (User not found)"), 'authorization', 'access');
						return false;
					}

					$this->user_id = $user_id;
					break;

				case 'api_key':
					if($this->url != 'v1/token/temp' && $this->url != 'v1/token/guest' && $this->url != 'v1/token/login')
					{
						debug::error(T_("Access denide to load this url by api key"), 'authorization', 'access');
						return false;
					}
					break;

				default :
					debug::error(T_("Invalid token"), 'authorization', 'access');
					return false;
			}

			if(isset(token::$PARENT['value']))
			{
				$this->parent_api_key = token::$PARENT['value'];
			}

			if(isset(token::$PARENT['user_id']))
			{
				$this->parent_api_key_user_id = token::$PARENT['user_id'];
			}
		}

		$this->authorization = $authorization;
	}

<commit_msg>api work by static token<commit_after><?php
namespace addons\content_api\v1\home;
use \lib\utility\permission;
use \lib\utility;
use \lib\debug;
use \addons\content_enter\main\tools\token;

class model extends \mvc\model
{

	/**
	 * the user id
	 *
	 * @var        integer
	 */
	public $user_id = null;


	/**
	 * the api is telegram bot
	 *
	 * @var        boolean
	 */
	public $telegram_api_mode = false;


	/**
	 * make debug return
	 * default is true
	 * in some where in site this method is false
	 *
	 * @var        boolean
	 */
	public $debug = true;


	/**
	 * the url
	 *
	 * @var        <type>
	 */
	public $url = null;


	/**
	 * the authorization
	 *
	 * @var        <type>
	 */
	public $authorization          = null;


	/**
	 * the parent api key
	 *
	 * @var        <type>
	 */
	public $parent_api_key         = null;
	public $parent_api_key_user_id = 0;


	use tools\_use;


	/**
	 * { function_description }
	 *
	 * @param      <type>  $_name  The name
	 * @param      <type>  $_args  The arguments
	 * @param      <type>  $parm   The parameter
	 *
	 * @return     <type>  ( description_of_the_return_value )
	 */
	function _call($_name, $_args, $parm)
	{
		$this->url = \lib\router::get_url();

		\lib\temp::set('api', true);

		$this->api_key();

		if(!debug::$status)
		{
			$this->_processor(['force_stop' => true]);
		}

		return parent::_call($_name, $_args, $parm);
	}


	/**
	 * check api key and set the user id
	 */
	public function api_key()
	{

		$api_token = utility::header("api_token") ? utility::header("api_token") : utility::header("Api_token");

		$authorization = utility::header("authorization");
		if($api_token)
		{
			$authorization = $api_token;
		}
		elseif(!$authorization)
		{
			$authorization = utility::header("Authorization");
		}

		if(!$authorization)
		{
			return debug::error('Authorization not found', 'authorization', 'access');
		}

		// static token list
		$static_token = \lib\option::config('enter', 'static_token');

		if($authorization === \lib\option::config('enter','telegram_hook'))
		{
			$this->telegram_api_mode = true;
			$this->telegram_token();
		}
		elseif(is_array($static_token) && in_array($authorization, $static_token))
		{
			// load user by mobile
			$this->static_token();
		}
		else
		{
			$token = token::get_type($authorization);

			if(!debug::$status)
			{
				return false;
			}

			switch ($token)
			{

				case 'user_token':
				case 'guest':
					if($this->url == 'v1/token/login' || $this->url == 'v1/token/guest')
					{
						debug::error(T_("Access denide (Invalid url)"), 'authorization', 'access');
						return false;
					}

					if(!token::check($authorization, $token))
					{
						return false;
					}

					$user_id = token::get_user_id($authorization);

					if(!$user_id)
					{
						debug::error(T_("Invalid authorization key (User not found)"), 'authorization', 'access');
						return false;
					}

					$this->user_id = $user_id;
					break;

				case 'api_key':
					if($this->url != 'v1/token/temp' && $this->url != 'v1/token/guest' && $this->url != 'v1/token/login')
					{
						debug::error(T_("Access denide to load this url by api key"), 'authorization', 'access');
						return false;
					}
					break;

				default :
					debug::error(T_("Invalid token"), 'authorization', 'access');
					return false;
			}

			if(isset(token::$PARENT['value']))
			{
				$this->parent_api_key = token::$PARENT['value'];
			}

			if(isset(token::$PARENT['user_id']))
			{
				$this->parent_api_key_user_id = token::$PARENT['user_id'];
			}
		}

		$this->authorization = $authorization;
	}


	/**
	 * the api telegram token
	 *
	 * @return     boolean  ( description_of_the_return_value )
	 */
	public function static_token()
	{
		$mobile = utility::header("mobile");

		$mobile = utility\filter::mobile($mobile);
		if(!$mobile)
		{
			debug::error(T_("Mobile not set"), 'mobile', 'header');
			return false;
		}

		$user_data = \lib\db\users::get_by_mobile($mobile);
		if(isset($user_data['id']))
		{
			$this->user_id = (int) $user_data['id'];
		}
		else
		{
			$signup        = ['mobile' => $mobile];
			$this->user_id = \lib\db\users::signup_quick($signup);
		}

		if(!$this->user_id)
		{
			\lib\db\logs::set('addons:api:static_token:user:not:found:register:faild');
			debug::error(T_("User not found and can not register the user"), 'static_token', 'header');
			return false;
		}

	}


{'fraction_non_alphanumeric': 0.17362722351121423, 'fraction_numerical': 0.0018045888115493685, 'mean_word_length': 2.8582794629537545, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4315185', 'n_tokens_mistral': 3253, 'n_tokens_neox': 2681, 'n_words': 531}