|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @package API |
| 4 | + * @version 1.5 |
| 5 | + * @author Brian Edgerton |
| 6 | + * @link http://www.edgewebworks.com |
| 7 | + * @copyright Copyright (C) 2011 Edge Web Works, LLC. All rights reserved. |
| 8 | + * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php |
| 9 | +*/ |
| 10 | + |
| 11 | +defined('_JEXEC') or die( 'Restricted access' ); |
| 12 | + |
| 13 | +jimport('joomla.plugin.plugin'); |
| 14 | +jimport('joomla.html.html'); |
| 15 | +jimport('joomla.user.helper'); |
| 16 | +jimport( 'joomla.application.component.helper' ); |
| 17 | +jimport( 'joomla.application.component.model' ); |
| 18 | +jimport( 'joomla.database.table.user' ); |
| 19 | + |
| 20 | +require_once( JPATH_SITE .DS.'libraries'.DS.'joomla'.DS.'filesystem'.DS.'folder.php'); |
| 21 | + |
| 22 | +/*if(JFolder::exists(JPATH_BASE.DS.'components'.DS.'com_xipt')) |
| 23 | +{ |
| 24 | +require_once( JPATH_SITE .DS.'components'.DS.'com_xipt'.DS.'api.xipt.php'); |
| 25 | +}*/ |
| 26 | + |
| 27 | +class AecApiResourceCreateuser extends ApiResource |
| 28 | +{ |
| 29 | + |
| 30 | + public function post() |
| 31 | + { |
| 32 | + |
| 33 | + $error_messages = array(); |
| 34 | + $fieldname = array(); |
| 35 | + $response = NULL; |
| 36 | + $validated = true; |
| 37 | + $userid = NULL; |
| 38 | + $data = JRequest::get('post'); |
| 39 | + |
| 40 | + //for rest api only |
| 41 | + unset($data['format']); |
| 42 | + unset($data['resource']); |
| 43 | + unset($data['app']); |
| 44 | + unset($data['key']); |
| 45 | + // |
| 46 | + //$userid = $data['userid']; |
| 47 | + //$fields = $data['field']; |
| 48 | +//print_r($_POST); die; |
| 49 | + //chk data |
| 50 | + if($data['email']=="" ) |
| 51 | + { |
| 52 | + $validated = false; |
| 53 | + $error_messages[] = array("success"=>0,"id"=>16,"fieldname"=>"email","message"=>"Email cannot be blank"); |
| 54 | + } elseif( false == $this->isValidEmail( $data['email'] ) ) { |
| 55 | + $validated = false; |
| 56 | + $error_messages[] = array( "success"=> 0, "id"=> 16, "fieldname"=> "email", "message"=> "Please set valid email id eg.([email protected]). Check 'email' field in request"); |
| 57 | + |
| 58 | + } |
| 59 | + if( $data['password']=="" ) |
| 60 | + { |
| 61 | + $validated = false; |
| 62 | + $error_messages[] = array("success"=>0,"id"=>15,"fieldname"=>"password","message"=>"Password cannot be blank"); |
| 63 | + } |
| 64 | + |
| 65 | + if( $data['name']=="" or $data['username']=="" ) |
| 66 | + { |
| 67 | + $validated = false; |
| 68 | + $error_messages[] = array("success"=>0,"id"=>14,"fieldname"=>"name/username","message"=>"Name cannot be blank"); |
| 69 | + } |
| 70 | + |
| 71 | + if( true == $validated) |
| 72 | + { //to create new user for joomla |
| 73 | + |
| 74 | + global $message; |
| 75 | + jimport('joomla.user.helper'); |
| 76 | + $authorize = & JFactory::getACL(); |
| 77 | + $user = clone(JFactory::getUser()); |
| 78 | + $user->set('username', $data['username']); |
| 79 | + $user->set('password', $data['password'] ); |
| 80 | + $user->set('name', $data['name']); |
| 81 | + $user->set('email', $data['email']); |
| 82 | + |
| 83 | + // password encryption |
| 84 | + $salt = JUserHelper::genRandomPassword(32); |
| 85 | + $crypt = JUserHelper::getCryptedPassword($user->password, $salt); |
| 86 | + $user->password = "$crypt:$salt"; |
| 87 | + |
| 88 | + // user group/type |
| 89 | + $user->set('id', ''); |
| 90 | + $user->set('usertype', 'Registered'); |
| 91 | + if(JVERSION >= '1.6.0') |
| 92 | + { |
| 93 | + $userConfig = JComponentHelper::getParams('com_users'); |
| 94 | + // Default to Registered. |
| 95 | + $defaultUserGroup = $userConfig->get('new_usertype', 2); |
| 96 | + $user->set('groups', array($defaultUserGroup)); |
| 97 | + } |
| 98 | + else |
| 99 | + $user->set('gid', $authorize->get_group_id( '', 'Registered', 'ARO' )); |
| 100 | + |
| 101 | + $date =& JFactory::getDate(); |
| 102 | + $user->set('registerDate', $date->toMySQL()); |
| 103 | + |
| 104 | + // true on success, false otherwise |
| 105 | + if(!$user->save()) |
| 106 | + { |
| 107 | + $message="not created because of ".$user->getError(); |
| 108 | + return false; |
| 109 | + } |
| 110 | + else |
| 111 | + { |
| 112 | + $message="created of username-".$user->username." and send mail of details please check"; |
| 113 | + |
| 114 | + } |
| 115 | + //$this->plugin->setResponse($user->id); |
| 116 | + $userid = $user->id; |
| 117 | + |
| 118 | + //result message |
| 119 | + $result = array('success'=>1,'user id '=>$userid,'username'=>$user->username,'message'=>$message); |
| 120 | + $result =($userid) ? $result : $message; |
| 121 | + |
| 122 | + $this->plugin->setResponse($result); |
| 123 | + |
| 124 | + |
| 125 | + } |
| 126 | + else |
| 127 | + { |
| 128 | + |
| 129 | + $this->plugin->setResponse($error_messages);//print_r($error_messages); die("validate mail2222"); |
| 130 | + } |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | + } |
| 135 | + |
| 136 | + function isValidEmail( $email ) |
| 137 | + { |
| 138 | + $pattern = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i"; |
| 139 | + |
| 140 | + if ( preg_match( $pattern, $email ) ) |
| 141 | + { |
| 142 | + return true; |
| 143 | + } else { |
| 144 | + return false; |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | +} |
0 commit comments